<?php
/**
 * Created by PhpStorm.
 * User: 图图
 * Date: 2021-6-8
 * Time: 16:29
 */

namespace app\web\controller;

use app\common\controller\Api;
use app\common\model\Area;
use app\common\model\Factor;
use app\common\model\Fenorder;
use app\common\model\Order as OrderModel;
use app\common\model\Goods;
use app\manage\admin\Group;
use app\web\controller\Express;
use MongoDB\Driver\Exception\AuthenticationException;
use think\Db;
use app\common\controller\Alipay;

/**
 * 技师控制器
 * @package app\index\controller
 */
class EngineerStatistics extends Auth
{
    public static $projectMoney = 0;//项目
    public static $projectCommission = 0;//项目提成
    public static $cardMoney = 0;//售卡金额
    public static $cardCommission = 0;//售卡提成
    public static $productMoney = 0;//商品金额
    public static $productCommission = 0;//商品提成
    public static $room_number = 0;//房间提成
    public static $user_number = 0;//客户数
    public static $averagePrice = 0;//客户均价
    public static $projectData = [];//项目数据
    public static $cardData = [];//卡数据
    public static $productData = [];//商品数据
    public $noNeedLogin = [];

    /**
     * 技师个人统计一
     */
    public function engineerOne()
    {
        if ($this->request->isPost()) {
            $getData = $this->request->post();
            $userInfo = $this->userInfo;
            if (!empty($getData['type'] == '1')) {
                $field = 'created_time';
                $leftField = 'cl.created_time';
            } else {
                $field = 'updated_time';
                $leftField = 'cl.updated_time';
            }
            if (empty($getData['start_time']) || empty($getData['end_time'])) {
                $this->result('缺少时间类型', '', 204);
            }
            $sql = Db::name('qt_commission_log')
                ->where(['store_id' => $userInfo['store_id']])
                ->whereBetweenTime($field, $getData['start_time'], $getData['end_time']);
                //->whereIn('type', ['1', '6']);
            //项目筛选
            if (!empty($getData['project_id'])) {
                $sql->where('product_id', $getData['project_id']);
            }
            //技师号筛选
            if (!empty($getData['work_number'])) {
                $engineerId = Db::name('qt_users')->where('work_number', $getData['work_number'])->value('id');
                $sql->where('user_id', $engineerId);

            }
            //工种筛选
            if (!empty($getData['work_type'])) {
                //获取工种所有的用户
                $workType = Db::name('qt_work_type')->where('id', $getData['work_type'])->find();
                if ($workType['pid'] == '0') {
                    //顶级分类查询所有的下级
                    $workRes = Db::name('qt_work_type')->where('pid', $getData['work_type'])->column('id');
                    $workTypeWhere[0] = $workType['id'];
                    $workRes = array_merge($workRes, $workTypeWhere);
                    $users = Db::name('qt_users')->whereIn('top_type_id', $workRes)->column('id');
                    $sql->whereIn('user_id', $users);
                } else {
                    $users = Db::name('qt_users')->where('top_type_id', $getData['work_type'])->column('id');
                    $sql->whereIn('user_id', $users);
                }
            }
            $userIds = $sql->group('user_id')->where('user_id', 'neq', 0)->column('user_id');
            //dump($userIds);die;
            $result = [];
            //处理总合计
            $total['project_name'] = '汇总合计';
            $total['row'] = 0;
            $total['row_commission'] = 0;
            $total['total_row'] = 0;
            $total['total_row_commission'] = 0;
            $total['total_commission'] = 0;
            $total['total_project_commission'] = 0;
            $total['achievement'] = 0;
            $total['total_number'] = 0;
            $total['add_row'] = 0;
            $total['choose'] = 0;
            $total['add_clock'] = 0;
            $total['clock_commission'] = 0;
            $total['total_choose'] = 0;
            $total['total_add'] = 0;
            $total['clock'] = 0;
            $total['total_clock'] = 0;
            $total['total_clock_commission'] = 0;
            $total['call'] = 0;
            $total['add_call'] = 0;
            $total['add'] = 0;
            $total['add_commission'] = 0;
            $total['add_add'] = 0;
            $total['adjunct'] = 0;
            $total['adjunct_commission'] = 0;
            foreach ($userIds as $key => $value) {
                $user = Db::name('qt_users')->field('id,work_number,real_name')->where('id', $value)->find();
                $result['data'][$key]['id'] = $user['id'];
                $result['data'][$key]['real_name'] = $user['real_name'];
                $result['data'][$key]['work_number'] = $user['work_number'];
                $result['data'][$key]['total_commission'] = 0;
                $result['data'][$key]['total_tuijian'] = 0;
                //售卡金额
                $result['data'][$key]['total_card_money'] = Db::name('qt_commission_log')
                    ->where(['store_id' => $userInfo['store_id']])
                    ->whereBetweenTime($field, $getData['start_time'], $getData['end_time'])
                    ->where(['user_id' => $user['id'], 'is_first' => '1'])
                    ->sum('money');
                //项目提成
                $result['data'][$key]['total_project_commission'] = Db::name('qt_commission_log')
                    ->where(['store_id' => $userInfo['store_id']])
                    ->whereBetweenTime($field, $getData['start_time'], $getData['end_time'])
                    ->where(['user_id' => $user['id'], 'type' => '1'])
                    ->sum('commission');

                $result['data'][$key]['total_commission'] = Db::name('qt_commission_log')
                    ->where(['store_id' => $userInfo['store_id']])
                    ->whereBetweenTime($field, $getData['start_time'], $getData['end_time'])
                    ->where(['user_id' => $user['id']])
                    ->sum('commission');
                //售卡提成
                $result['data'][$key]['total_card_commission'] = Db::name('qt_commission_log')
                    ->where(['store_id' => $userInfo['store_id']])
                    ->whereBetweenTime($field, $getData['start_time'], $getData['end_time'])
                    ->where(['user_id' => $user['id']])
                    ->whereIn('type',['3','4','5','7'])
                    ->sum('commission');
                //商品提成
                $result['data'][$key]['total_goods_commission'] = Db::name('qt_commission_log')
                    ->where(['store_id' => $userInfo['store_id']])
                    ->whereBetweenTime($field, $getData['start_time'], $getData['end_time'])
                    ->where(['user_id' => $user['id'], 'type' => '2'])
                    ->sum('commission');
                //查询当前用户所有的项目
                $data = Db::name('qt_commission_log')
                    ->field('cl.product_id as project_id,p.name as project_name,cl.status,op.is_add,cl.commission,cl.payment_price as money,cl.type')
                    ->alias('cl')
                    ->leftJoin('qt_project p', 'p.id = cl.product_id')
                    ->leftJoin('qt_order_project op', 'op.id = cl.order_project_id')
                    ->where(['cl.store_id' => $userInfo['store_id']])
                    ->whereBetweenTime($leftField, $getData['start_time'], $getData['end_time'])
                    ->where(['cl.user_id' => $user['id']])
                    ->whereIn('cl.type', ['1', '6'])
                    ->select();
                $project = [];
                foreach ($data as $k => $v) {
                    $project[$v['project_id']]['project_name'] = Db::name('qt_project')->where('id', $v['project_id'])->value('name');
                    $project[$v['project_id']]['row'] = 0;
                    $project[$v['project_id']]['row_commission'] = 0;
                    $project[$v['project_id']]['total_row'] = 0;
                    $project[$v['project_id']]['total_row_commission'] = 0;
                    $project[$v['project_id']]['total_commission'] = 0;
                    $project[$v['project_id']]['achievement'] = 0;
                    $project[$v['project_id']]['total_number'] = 0;
                    $project[$v['project_id']]['add_row'] = 0;
                    $project[$v['project_id']]['choose'] = 0;
                    $project[$v['project_id']]['add_choose'] = 0;
                    $project[$v['project_id']]['total_choose'] = 0;
                    $project[$v['project_id']]['total_add'] = 0;
                    $project[$v['project_id']]['clock'] = 0;
                    $project[$v['project_id']]['add_clock'] = 0;
                    $project[$v['project_id']]['total_clock'] = 0;
                    $project[$v['project_id']]['clock_commission'] = 0;
                    $project[$v['project_id']]['total_clock_commission'] = 0;
                    $project[$v['project_id']]['call'] = 0;
                    $project[$v['project_id']]['add_call'] = 0;
                    $project[$v['project_id']]['call_commission'] = 0;
                    $project[$v['project_id']]['add'] = 0;
                    $project[$v['project_id']]['add_commission'] = 0;
                    $project[$v['project_id']]['add_add'] = 0;
                    $project[$v['project_id']]['adjunct'] = 0;
                    $project[$v['project_id']]['adjunct_commission'] = 0;

                }
                foreach ($data as $k => $v) {
                    //排钟数量
                    if ($v['status'] == '1' && $v['is_add'] == '1') {
                        $project[$v['project_id']]['row'] += 1;
                        $project[$v['project_id']]['total_row'] += 1;
                        $project[$v['project_id']]['total_row_commission'] += $v['commission'];//排钟提成
                        $project[$v['project_id']]['total_commission'] += $v['commission'];//总提成
                        $project[$v['project_id']]['achievement'] += $v['money'];//业绩
                        $project[$v['project_id']]['total_number'] += 1;//总提成数量

                    }
                    //加班排钟数量
                    if ($v['status'] == '1' && $v['is_add'] == '2') {
                        $project[$v['project_id']]['add_row'] += 1;
                        $project[$v['project_id']]['total_row'] += 1;
                        $project[$v['project_id']]['total_row_commission'] += $v['commission'];//排钟提成
                        $project[$v['project_id']]['total_commission'] += $v['commission'];//总提成
                        $project[$v['project_id']]['total_add'] += 1;//加班合计
                        $project[$v['project_id']]['achievement'] += $v['money'];//业绩
                        $project[$v['project_id']]['total_number'] += 1;//总提成数量
                    }
                    //选钟数量
                    if ($v['status'] == '4' && $v['is_add'] == '1') {
                        $project[$v['project_id']]['choose'] += 1;
                        $project[$v['project_id']]['total_row'] += 1;
                        $project[$v['project_id']]['total_row_commission'] += $v['commission'];
                        $project[$v['project_id']]['total_commission'] += $v['commission'];//总提成
                        $project[$v['project_id']]['total_choose'] += 1;//选钟合计
                        $project[$v['project_id']]['achievement'] += $v['money'];//业绩
                        $project[$v['project_id']]['total_number'] += 1;//总提成数量

                    }
                    //加班选钟数量
                    if ($v['status'] == '4' && $v['is_add'] == '2') {
                        $project[$v['project_id']]['add_choose'] += 1;
                        $project[$v['project_id']]['total_row'] += 1;
                        $project[$v['project_id']]['total_row_commission'] += $v['commission'];
                        $project[$v['project_id']]['total_commission'] += $v['commission'];//总提成
                        $project[$v['project_id']]['total_add'] += 1;//加班合计
                        $project[$v['project_id']]['total_choose'] += 1;//选钟合计
                        $project[$v['project_id']]['achievement'] += $v['money'];//业绩
                        $project[$v['project_id']]['total_number'] += 1;//总提成数量
                    }
                    //点钟数量
                    if ($v['status'] == '2' && $v['is_add'] == '1') {
                        $project[$v['project_id']]['clock'] += 1;
                        $project[$v['project_id']]['total_clock'] += 1;//点钟数量合计
                        $project[$v['project_id']]['total_clock_commission'] += $v['commission'];//点钟提成合计
                        $project[$v['project_id']]['total_commission'] += $v['commission'];//总提成
                        $project[$v['project_id']]['achievement'] += $v['money'];//业绩
                        $project[$v['project_id']]['total_number'] += 1;//总提成数量
                    }
                    //加班点钟数量
                    if ($v['status'] == '2' && $v['is_add'] == '2') {
                        $project[$v['project_id']]['add_clock'] += 1;
                        $project[$v['project_id']]['total_clock'] += 1;//点钟数量合计
                        $project[$v['project_id']]['total_clock_commission'] += $v['commission'];//点钟提成合计
                        $project[$v['project_id']]['total_commission'] += $v['commission'];//总提成
                        $project[$v['project_id']]['total_add'] += 1;//加班合计
                        $project[$v['project_id']]['achievement'] += $v['money'];//业绩
                        $project[$v['project_id']]['total_number'] += 1;//总提成数量
                    }

                    //call数量
                    if ($v['status'] == '3' && $v['is_add'] == '1') {
                        $project[$v['project_id']]['call'] += 1;
                        $project[$v['project_id']]['call_commission'] += $v['commission'];//call钟提成
                        $project[$v['project_id']]['total_commission'] += $v['commission'];//总提成
                        $project[$v['project_id']]['achievement'] += $v['money'];//业绩
                        $project[$v['project_id']]['total_number'] += 1;//总提成数量
                    }
                    //加班call钟数量
                    if ($v['status'] == '3' && $v['is_add'] == '2') {
                        $project[$v['project_id']]['add_call'] += 1;
                        $project[$v['project_id']]['total_commission'] += $v['commission'];//总提成
                        $project[$v['project_id']]['achievement'] += $v['money'];//业绩
                        $project[$v['project_id']]['total_number'] += 1;//总提成数量
                    }
                    //加钟数量
                    if ($v['status'] == '5' && $v['is_add'] == '1') {
                        $project[$v['project_id']]['add'] += 1;
                        $project[$v['project_id']]['add_commission'] += $v['commission'];
                        $project[$v['project_id']]['total_commission'] += $v['commission'];//总提成
                        $project[$v['project_id']]['achievement'] += $v['money'];//业绩
                        $project[$v['project_id']]['total_number'] += 1;//总提成数量
                    }
                    //加班加钟数量
                    if ($v['status'] == '3' && $v['is_add'] == '2') {
                        $project[$v['project_id']]['add_add'] += 1;
                        $project[$v['project_id']]['total_commission'] += $v['commission'];//总提成
                        $project[$v['project_id']]['achievement'] += $v['money'];//业绩
                    }
                    //副项数量
                    if ($v['type'] == '6') {
                        $project[$v['project_id']]['adjunct'] += 1;
                        $project[$v['project_id']]['adjunct_commission'] += $v['commission'];//副项提成
                        $project[$v['project_id']]['total_commission'] += $v['commission'];//总提成
                        $project[$v['project_id']]['achievement'] += $v['money'];//业绩
                        $project[$v['project_id']]['total_number'] += 1;//总提成数量
                    }
                    $total['project_name'] = '汇总合计';
                    $total['row'] += $project[$v['project_id']]['row'];
                    $total['total_row'] += $project[$v['project_id']]['total_row'];
                    $total['total_row_commission'] += $project[$v['project_id']]['total_row_commission'];
                    $total['achievement'] += $project[$v['project_id']]['achievement'];
                    $total['total_number'] += $project[$v['project_id']]['total_number'];
                    $total['add_row'] += $project[$v['project_id']]['add_row'];
                    $total['choose'] += $project[$v['project_id']]['choose'];
                    $total['total_choose'] += $project[$v['project_id']]['total_choose'];
                    $total['total_add'] += $project[$v['project_id']]['total_add'];
                    $total['clock'] += $project[$v['project_id']]['clock'];
                    $total['add_clock'] += $project[$v['project_id']]['add_clock'];
                    $total['clock_commission'] += $project[$v['project_id']]['clock_commission'];
                    $total['total_clock'] += $project[$v['project_id']]['total_clock'];
                    $total['call'] += $project[$v['project_id']]['call'];
                    $total['add_call'] += $project[$v['project_id']]['add_call'];
                    $total['add'] += $project[$v['project_id']]['add'];
                    $total['add_commission'] += $project[$v['project_id']]['add_commission'];
                    $total['add_add'] += $project[$v['project_id']]['add_add'];
                    $total['adjunct'] += $project[$v['project_id']]['adjunct'];
                    $total['adjunct_commission'] += $project[$v['project_id']]['adjunct_commission'];
                }
                //处理单个合计
                $singleTotal['project_name'] = '合计';
                $singleTotal['row'] = 0;
                $singleTotal['total_row'] = 0;
                $singleTotal['total_row_commission'] = 0;
                $singleTotal['achievement'] = 0;
                $singleTotal['total_number'] = 0;
                $singleTotal['add_row'] = 0;
                $singleTotal['choose'] = 0;
                $singleTotal['total_choose'] = 0;
                $singleTotal['total_add'] = 0;
                $singleTotal['clock'] = 0;
                $singleTotal['add_clock'] = 0;
                $singleTotal['clock_commission'] = 0;
                $singleTotal['total_clock'] = 0;
                $singleTotal['call'] = 0;
                $singleTotal['add_call'] = 0;
                $singleTotal['add'] = 0;
                $singleTotal['add_commission'] = 0;
                $singleTotal['add_add'] = 0;
                $singleTotal['adjunct'] = 0;
                $singleTotal['adjunct_commission'] = 0;
                foreach ($project as $k => $v) {
                    $singleTotal['row'] += $v['row'];
                    $singleTotal['total_row'] += $v['total_row'];
                    $singleTotal['total_row_commission'] += $v['total_row_commission'];
                    $singleTotal['achievement'] += $v['achievement'];
                    $singleTotal['total_number'] += $v['total_number'];
                    $singleTotal['add_row'] += $v['add_row'];
                    $singleTotal['choose'] += $v['choose'];
                    $singleTotal['total_choose'] += $v['total_choose'];
                    $singleTotal['total_add'] += $v['total_add'];
                    $singleTotal['clock'] += $v['clock'];
                    $singleTotal['total_clock'] += $v['total_clock'];
                    $singleTotal['call'] += $v['call'];
                    $singleTotal['add_call'] += $v['add_call'];
                    $singleTotal['add'] += $v['add'];
                    $singleTotal['add_commission'] += $v['add_commission'];
                    $singleTotal['add_add'] += $v['add_add'];
                    $singleTotal['adjunct'] += $v['adjunct'];
                    $singleTotal['adjunct_commission'] += $v['adjunct_commission'];
                }
                $project = array_values($project);
                $count = count($project);
                $project[$count] = $singleTotal;
                $result['data'][$key]['project'] = $project;
            }
            $result['total'] = $total;
            $this->result('查询成功', $result, 200);
        }
        $this->result('无效的请求', '', 204);

    }


    /**
     * 技师个人统计三
     */
    public function engineerThree()
    {
        if ($this->request->isPost()) {
            $getData = $this->request->post();
            $userInfo = $this->userInfo;
            if (!empty($getData['type'] == '1')) {
                $field = 'created_time';
            } else {
                $field = 'updated_time';
            }
            if (empty($getData['start_time']) || empty($getData['end_time'])) {
                $this->result('缺少时间类型', '', 204);
            }

            $sql = Db::name('qt_commission_log')
                ->where(['store_id' => $userInfo['store_id']])
                ->whereBetweenTime($field, $getData['start_time'], $getData['end_time'])
                ->whereIn('type', ['1', '6'])
                ->where('product_id','neq','0');
            //项目筛选
            if (!empty($getData['project_id'])) {
                $sql->where('product_id', $getData['project_id']);
            }
            //技师号筛选
            if (!empty($getData['work_number'])) {
                $engineerId = Db::name('qt_users')->where('work_number', $getData['work_number'])->value('id');
                $sql->where('user_id', $engineerId);
            }
            //工种筛选
            if (!empty($getData['work_type'])) {
                //获取工种所有的用户
                $workType = Db::name('qt_work_type')->where('id', $getData['work_type'])->find();
                if ($workType['pid'] == '0') {
                    //顶级分类查询所有的下级
                    $workRes = Db::name('qt_work_type')->where('pid', $getData['work_type'])->column('id');
                    $workTypeWhere[0] = $workType['id'];
                    $workRes = array_merge($workRes, $workTypeWhere);
                    $users = Db::name('qt_users')->whereIn('work_type', $workRes)->column('id');
                    $sql->whereIn('user_id', $users);
                } else {
                    $users = Db::name('qt_users')->where('work_type', $getData['work_type'])->column('id');
                    $sql->whereIn('user_id', $users);
                }
            }
            //用户ID合集
            $userIds = $sql->group('user_id')->where('user_id', 'neq', 0)->column('user_id');
            //项目ID合集
            $projectIds = $sql->group('product_id')->column('product_id');
            $result = [];
            $insertField = [];
            //表头项目
            $projectRes = Db::name('qt_project')->field('id,name')->whereIn('id', $projectIds)->select();
            $result['project'] = $projectRes;
            foreach ($projectRes as $key => $value) {
                $insertField[$value['id']] = 0;
            }
            $total = [
                'total_add' => 0,//加钟总数
                'total_spot' => 0,//点钟总数
                'total_add_spot' => 0,//加班点钟总数
                'total_choose' => 0,//选钟总数
                'total_add_choose' => 0,//加班选钟总数
                'total_row' => 0,//排钟总数
                'total_add_row' => 0,//加班排钟总数
                'total_clock' => 0,//总钟数
                'total_tuijian' => 0,//推荐
                'total_adjunct' => 0,//副项
            ];
            $total = $total + $insertField;
            foreach ($userIds as $key => $value) {
                $user = Db::name('qt_users')->field('id,work_number,real_name')->where('id', $value)->find();
                $result['data'][$key] = [
                    'id' => $user['id'],
                    'real_name' => $user['real_name'],
                    'work_number' => $user['work_number'],
                    'total_add' => 0,//加钟总数
                    'total_spot' => 0,//点钟总数
                    'total_add_spot' => 0,//加班点钟总数
                    'total_choose' => 0,//选钟总数
                    'total_add_choose' => 0,//加班选钟总数
                    'total_row' => 0,//排钟总数
                    'total_add_row' => 0,//加班排钟总数
                    'total_clock' => 0,//总钟数
                    'total_tuijian' => 0,//推荐
                    'total_adjunct' => 0,//副项
                ];
                $result['data'][$key] = $result['data'][$key] + $insertField;
                //查询当前用户所有的项目
                $data = Db::name('qt_commission_log')
                    ->field('cl.product_id as project_id,p.name as project_name,cl.status,cl.is_add,cl.commission,cl.money,cl.type')
                    ->alias('cl')
                    ->leftJoin('qt_project p', 'p.id = cl.product_id')
                    ->where(['cl.store_id' => $userInfo['store_id']])
                    ->whereBetweenTime($field, $getData['start_time'], $getData['end_time'])
                    ->where(['cl.user_id' => $user['id']])
                    ->whereIn('cl.type', ['1', '6'])
                    ->select();
                foreach ($data as $k => $v) {
                    //加钟数量
                    if ($v['status'] == '5') {
                        $result['data'][$key]['total_add'] += 1;
                        $result['data'][$key]['total_clock'] += 1;//总钟数
                        $total['total_add'] += 1;
                        $total['total_clock'] += 1;

                    }
                    //点钟数量
                    if ($v['status'] == '2' && $v['is_add'] == '1') {
                        $result['data'][$key]['total_spot'] += 1;
                        $result['data'][$key]['total_clock'] += 1;//总钟数
                        $total['total_spot'] += 1;
                        $total['total_clock'] += 1;

                    }
                    //加班点钟数量
                    if ($v['status'] == '2' && $v['is_add'] == '2') {
                        $result['data'][$key]['total_add_spot'] += 1;
                        $result['data'][$key]['total_clock'] += 1;//总钟数
                        $total['total_add_spot'] += 1;
                        $total['total_clock'] += 1;

                    }
                    //选钟数量
                    if ($v['status'] == '4' && $v['is_add'] == '1') {
                        $result['data'][$key]['total_choose'] += 1;
                        $result['data'][$key]['total_clock'] += 1;//总钟数
                        $total['total_choose'] += 1;
                        $total['total_clock'] += 1;

                    }
                    //加班选钟数量
                    if ($v['status'] == '4' && $v['is_add'] == '2') {
                        $result['data'][$key]['total_add_choose'] += 1;
                        $result['data'][$key]['total_clock'] += 1;//总钟数
                        $total['total_add_choose'] += 1;
                        $total['total_clock'] += 1;

                    }
                    //排钟
                    if ($v['status'] == '1' && $v['is_add'] == '1') {
                        $result['data'][$key]['total_row'] += 1;
                        $result['data'][$key]['total_clock'] += 1;//总钟数
                        $total['total_row'] += 1;
                        $total['total_clock'] += 1;

                    }
                    //加班排钟
                    if ($v['status'] == '1' && $v['is_add'] == '2') {
                        $result['data'][$key]['total_add_row'] += 1;
                        $result['data'][$key]['total_clock'] += 1;//总钟数
                        $total['total_add_row'] += 1;
                        $total['total_clock'] += 1;

                    }
                    //副项
                    if ($v['type'] == '6') {
                        $result['data'][$key]['total_adjunct'] += 1;
                        $total['total_adjunct'] += 1;
                    }
                    if (in_array($v['project_id'], $projectIds)) {
                        $result['data'][$key][$v['project_id']] += 1;
                        $total[$v['project_id']] += 1;
                    }
                }
            }
            $result['total'] = $total;
            $this->result('查询成功', $result, 200);
        }
    }


    /**
     * 技师实收业绩报表
     */
    public function netReceipts()
    {
        if ($this->request->isPost()) {
            $getData = $this->request->post();
            $userInfo = $this->userInfo;
            if (!empty($getData['type'] == '1')) {
                $field = 'created_time';
            } else {
                $field = 'updated_time';
            }
            if (empty($getData['start_time']) || empty($getData['end_time'])) {
                $this->result('缺少时间类型', '', 204);
            }

            $sql = Db::name('qt_commission_log')
                ->where(['store_id' => $userInfo['store_id']])
                ->whereBetweenTime($field, $getData['start_time'], $getData['end_time']);
            //用户ID合集
            $userIds = $sql->group('user_id')->where('user_id', 'neq', 0)->where('payment_price', 'neq',0)->column('user_id');
            $result = [];
            foreach ($userIds as $key => $value) {
                $user = Db::name('qt_users')->field('id,work_number,real_name')->where('id', $value)->find();
                if (empty($user)) {
                    continue;
                }
                $result['data'][$key] = [
                    'id' => $user['id'],
                    'real_name' => $user['real_name'],
                    'work_number' => $user['work_number'],
                    'project_achievement' => 0,//项目业绩
                    'project_number' => 0,//项目数量
                    'project_commission' => 0,//项目提成

                    'goods_achievement' => 0,//商品业绩
                    'goods_number' => 0,//商品总数
                    'goods_commission' => 0,//商品提成

                    'card_achievement' => 0,//售卡业绩
                    'card_number' => 0,//售卡总数
                    'card_commission' => 0,//售卡提成

                    'tuijian' => 0,//推荐提成
                    'total_commission' => 0,//总提成
                    'total_achievement' => 0,//总业绩
                ];
                static $product = [];
                $i = 0;
                //查询当前用户所有的项目
                $data = Db::name('qt_commission_log')
                    ->field('cl.product_id as project_id,p.name as project_name,cl.status,cl.is_add,cl.commission,cl.money,cl.type,cl.order_project_id')
                    ->alias('cl')
                    ->leftJoin('qt_project p', 'p.id = cl.product_id')
                    ->where(['cl.store_id' => $userInfo['store_id']])
                    ->whereBetweenTime($field, $getData['start_time'],$getData['end_time'])
                    ->where(['cl.user_id' => $user['id']])
                    ->select();
                if (!empty($data)) {
                    foreach ($data as $k => $v) {
                        //项目业绩，数量，提成
                        if ($v['type'] == '1') {
                            $result['data'][$key]['project_achievement'] += $v['money'];
                            $result['data'][$key]['project_number'] += 1;
                            $result['data'][$key]['project_commission'] += $v['commission'];
                            $result['data'][$key]['total_commission'] += $v['commission'];
                            $result['data'][$key]['total_achievement'] += $v['money'];
                            $result['data'][$key]['product'][$i]['id'] = $v['project_id'];
                            $result['data'][$key]['product'][$i]['type'] = '1';

                        }
                        //商品业绩，数量，提成
                        if ($v['type'] == '2') {
                            $result['data'][$key]['goods_achievement'] += $v['money'];
                            $goodsNum = Db::name('qt_product_order_item')->where(['id'=>$v['order_project_id']])->value('goods_num');
                            $result['data'][$key]['goods_number'] += $goodsNum;
                            $result['data'][$key]['goods_commission'] += $v['commission'];
                            $result['data'][$key]['total_commission'] += $v['commission'];
                            $result['data'][$key]['total_achievement'] += $v['money'];
                            $result['data'][$key]['product'][$i]['id'] = $v['project_id'];
                            $result['data'][$key]['product'][$i]['type'] = '2';
                            $result['data'][$key]['product'][$i]['number'] =$goodsNum;

                        }
                        //售卡业绩，数量，提成
                        if ($v['type'] == '3' || $v['type'] == '4' || $v['type'] == '5' || $v['type'] == '7') {
                            $result['data'][$key]['card_achievement'] += $v['money'];
                            $result['data'][$key]['card_number'] += 1;
                            $result['data'][$key]['card_commission'] += $v['commission'];
                            $result['data'][$key]['total_commission'] += $v['commission'];
                            $result['data'][$key]['total_achievement'] += $v['money'];
                            $result['data'][$key]['product'][$i]['id'] = $v['project_id'];
                            $result['data'][$key]['product'][$i]['type'] = '3';

                        }
                        $i++;
                    }
                    $projectIds = [];
                    $product = $result['data'][$key]['product'];
                    foreach ($product as $k => $v) {
                        if ($v['type'] == '1') {
                            $name = Db::name('qt_project')->where('id', $v['id'])->value('name');
                            $product[$k]['name'] = $name;
                            $projectIds[$k] = 'project' . '-' . $v['id'];
                        }
                        if ($v['type'] == '2') {
                            $name = Db::name('qt_goods')->where('id', $v['id'])->value('name');
                            $product[$k]['name'] = $name;
                            $projectIds[$k] = 'goods' . '-' . $v['id'];
                        }
                        if ($v['type'] == '3') {
                            $name = Db::name('qt_viper_card')->where('id', $v['id'])->value('name');
                            $product[$k]['name'] = $name;
                            $projectIds[$k] = 'card' . '-' . $v['id'];
                        }
                        $product[$k]['count'] = 0;
                    }
                    foreach ($product as $k => $v) {
                        if ($v['type'] == '1') {
                            $projectId = 'project' . '-' . $v['id'];
                            if (in_array($projectId, $projectIds)) {
                                $product[$k]['count'] += 1;
                            }
                        }
                        if ($v['type'] == '2') {
                            $projectId = 'goods' . '-' . $v['id'];
                            if (in_array($projectId, $projectIds)) {
                                $product[$k]['count'] += $v['number'];
                            }
                        }
                        if ($v['type'] == '3') {
                            $projectId = 'card' . '-' . $v['id'];
                            if (in_array($projectId, $projectIds)) {
                                $product[$k]['count'] += 1;
                            }
                        }
                    }
                    //处理数据
                    $productNameCount = '';
                    foreach ($product as $k => $v) {
                        $productNameCount .= $v['name'] . '(' . $v['count'] . ')，';
                    }
                    unset($result['data'][$key]['product']);
                    $result['data'][$key]['name_count'] = $productNameCount;
                }

            }
            //计算总提成
            $total['total_commission'] = $sql = Db::name('qt_commission_log')
                ->where(['store_id' => $userInfo['store_id']])
                ->whereBetweenTime($field, $getData['start_time'], $getData['end_time'])
                ->sum('commission');

            //计算总业绩
            $total['total_achievement'] = $sql = Db::name('qt_commission_log')
                ->where(['store_id' => $userInfo['store_id']])
                ->whereBetweenTime($field, $getData['start_time'], $getData['end_time'])
                ->sum('money');
            //计算总卡金额
            $total['total_card'] = $sql = Db::name('qt_commission_log')
                ->where(['store_id' => $userInfo['store_id']])
                ->whereBetweenTime($field, $getData['start_time'], $getData['end_time'])
                ->whereIn('type', ['3', '4', '5', '7'])
                ->sum('money');
            //计算总钟数
            $total['total_clock'] = $sql = Db::name('qt_commission_log')
                ->where(['store_id' => $userInfo['store_id']])
                ->whereBetweenTime($field, $getData['start_time'], $getData['end_time'])
                ->where('type', '1')
                ->count();
            $result['total'] = $total;
            $this->result('查询成功', $result, 200);
        }
    }


    /**
     * 技师实收业绩报表设置
     */
    public function getNetReceiptsConfig()
    {
        if ($this->request->isPost()) {
            $data = Db::name('qt_check_out_config')->field('id,product_name,product_price,product_rate,type')->where(['store_id' => $this->userInfo['store_id']])->select();
            if (empty($data)) {
                $project = Db::name('qt_project')
                    ->field('id as product_id,price as product_price,name as product_name')
                    ->where(['store_id' => $this->userInfo['store_id']])
                    ->select();
                foreach ($project as $key => $value) {
                    $project[$key]['id'] = '';
                    $project[$key]['type'] = '1';
                    $project[$key]['product_rate'] = 0;
                }

                $goods = Db::name('qt_goods')
                    ->field('id as product_id,price as product_price,name as product_name')
                    ->where(['store_id' => $this->userInfo['store_id']])
                    ->select();
                foreach ($project as $key => $value) {
                    $goods[$key]['id'] = '';
                    $goods[$key]['type'] = '2';
                    $goods[$key]['product_rate'] = 0;
                }
                $data = array_merge($project, $goods);

            }
            $this->result('查询成功', $data, 200);
        }
    }


    /**
     * 技师实收业绩报表设置
     */
    public function editNetReceiptsConfig()
    {
        if ($this->request->isPost()) {
            $getData = $this->request->post();
            if (empty($getData['config_data'])) {
                $configData = json_decode($getData['config_data'], true);
                foreach ($configData as $key => $value) {
                    if ($value['id'] == '') {
                        $check = Db::name('qt_check_out_config')
                            ->where(['store_id' => $this->userInfo['store_id'], 'type' => $value['type'], 'product_id' => $value['product_id']])->find();
                        if (empty($check)) {
                            Db::name('qt_check_out_config')->insert($value);
                        }
                    } else {
                        Db::name('qt_check_out_config')->where(['id' => $value['id']])->update($value);
                    }
                }
            }
            $this->result('修改成功', '', 200);
        }
    }


    /**
     * 技师业绩明细报表
     */
    public function netReceiptsDetail()
    {
        if ($this->request->isPost()) {
            $getData = $this->request->post();
            $userInfo = $this->userInfo;
            if (!empty($getData['type'] == '1')) {
                $field = 'log.created_time';
                $dataFiled = 'cl.created_time';
            } else {
                $field = 'log.updated_time';
                $dataFiled = 'cl.updated_time';
            }
            if (empty($getData['start_time']) || empty($getData['end_time'])) {
                $this->result('缺少时间类型', '', 204);
            }

            $sql = Db::name('qt_commission_log')
                ->alias('log')
                ->leftJoin('qt_users u', 'u.id = log.user_id')
                ->where(['log.store_id' => $userInfo['store_id']])
                ->whereBetweenTime($field, $getData['start_time'], $getData['end_time']);
            //技师号筛选
            if (!empty($getData['work_number'])) {
                $sql->where('u.work_number', $getData['work_number']);
            }
            //技师工种筛选
            if (!empty($getData['top_type_id'])) {
                $sql->where('u.top_type_id', $getData['top_type_id']);
            }
            //用户ID合集
            $userIds = $sql->group('log.user_id')->where('log.user_id', 'neq', 0)->where('log.payment_price', 'neq',0)->column('log.user_id');
            $result = [];
            $total = [
                'project_achievement' => 0,//项目业绩
                'project_number' => 0,//项目数量
                'project_commission' => 0,//项目提成
                'goods_achievement' => 0,//商品业绩
                'goods_commission' => 0,//商品提成
                'card_achievement' => 0,//售卡业绩
                'card_number' => 0,//售卡总数
                'card_commission' => 0,//售卡提成
                'tuijian' => 0,//推荐提成
                'total_commission' => 0,//总提成
                'total_achievement' => 0,//总业绩
                'total_all' => 0,//排钟
                'total_spot' => 0,//点钟
                'total_call' => 0,//call钟
                'total_add' => 0,//加钟
                'total_add_work' => 0,//加班
                'total_adjunct' => 0,//副项
                'total_main' => 0,//主项合计
                'total_adjunct_project' => 0,//副项合计
                'total_project' => 0,//总合计
            ];
            foreach ($userIds as $key => $value) {
                $user = Db::name('qt_users')->field('id,work_number,real_name')->where('id', $value)->find();
                $result['data'][$key] = [
                    'id' => $user['id'],
                    'real_name' => $user['real_name'],
                    'work_number' => $user['work_number'],
                    'project_achievement' => 0,//项目业绩
                    'project_number' => 0,//项目数量
                    'project_commission' => 0,//项目提成
                    'goods_achievement' => 0,//商品业绩
                    'goods_commission' => 0,//商品提成
                    'card_achievement' => 0,//售卡业绩
                    'card_number' => 0,//售卡总数
                    'card_commission' => 0,//售卡提成
                    'tuijian' => 0,//推荐提成
                    'total_commission' => 0,//总提成
                    'total_achievement' => 0,//总业绩
                    'total_all' => 0,//排钟
                    'total_spot' => 0,//点钟
                    'total_call' => 0,//call钟
                    'total_add' => 0,//加钟
                    'total_add_work' => 0,//加班
                    'total_adjunct' => 0,//副项
                    'total_main' => 0,//主项合计
                    'total_adjunct_project' => 0,//副项合计
                    'total_project' => 0,//总合计
                ];
                static $product = [];
                $i = 0;
                //查询当前用户所有的数据
                $data = Db::name('qt_commission_log')
                    ->field('cl.product_id as project_id,p.name as project_name,cl.status,cl.is_add,cl.commission,cl.payment_price as money,cl.type,cl.is_first,cl.order_project_id,item.goods_num,cl.order_project_id')
                    ->alias('cl')
                    ->leftJoin('qt_project p', 'p.id = cl.product_id')
                    ->leftJoin('qt_product_order_item item', 'cl.order_project_id = item.id')
                    ->where(['cl.store_id' => $userInfo['store_id']])
                    ->whereBetweenTime($dataFiled, $getData['start_time'],$getData['end_time'])
                    ->where(['cl.user_id' => $user['id']])
                    ->where('cl.user_id','neq','0')
                    ->where('cl.payment_price', 'neq','0')
                    ->select();
                foreach ($data as $k => $v) {
                    $result['data'][$key]['goods_num'][$v['project_id']] = 0;
                    //项目业绩，数量，提成
                    if ($v['type'] == '1') {
                        $result['data'][$key]['project_achievement'] += $v['money'];
                        $total['project_achievement'] += $v['money'];
                        $result['data'][$key]['project_number'] += 1;
                        $total['project_number'] += 1;
                        $result['data'][$key]['project_commission'] += $v['commission'];
                        $total['project_commission'] += $v['commission'];
                        $result['data'][$key]['total_commission'] += $v['commission'];
                        $total['total_commission'] += $v['commission'];
                        $result['data'][$key]['total_achievement'] += $v['money'];
                        $total['total_achievement'] += $v['money'];
                        $result['data'][$key]['project'][$i] = $v['project_id'];
                        //处理主副项合计
                        if ($v['status'] != '6') {
                            $result['data'][$key]['total_project'] += 1;
                            $total['total_project'] += 1;
                        } else {
                            $result['data'][$key]['total_adjunct'] += 1;
                            $total['total_adjunct'] += 1;
                            $result['data'][$key]['total_adjunct_project'] += 1;
                            $total['total_adjunct_project'] += 1;
                        }
                        //所有项目合计
                        $result['data'][$key]['total_project'] += 1;
                        $total['total_project'] += 1;
                        //轮排
                        if ($v['status'] == '1' && $v['is_add'] == '1') {
                            $result['data'][$key]['total_all'] += 1;
                            $total['total_all'] += 1;
                        }
                        //call钟
                        if ($v['status'] == '3' && $v['is_add'] == '1') {
                            $result['data'][$key]['total_call'] += 1;
                            $total['total_call'] += 1;
                        }

                        //点钟
                        if ($v['status'] == '2' && $v['is_add'] == '1') {
                            $result['data'][$key]['total_spot'] += 1;
                            $total['total_spot'] += 1;
                        }
                        //加钟
                        if ($v['status'] == '5' && $v['is_add'] == '1') {
                            $result['data'][$key]['total_add'] += 1;
                            $total['total_add'] += 1;
                        }
                        //加班
                        if ($v['status'] == '1' && $v['is_add'] == '2') {
                            $result['data'][$key]['total_add_work'] += 1;
                            $total['total_add_work'] += 1;
                        }
                        if ($v['status'] == '4' && $v['is_add'] == '2') {
                            $result['data'][$key]['total_add_work'] += 1;
                            $total['total_add_work'] += 1;
                        }
                        if ($v['status'] == '2' && $v['is_add'] == '2') {
                            $result['data'][$key]['total_add_work'] += 1;
                            $total['total_add_work'] += 1;
                        }
                    }
                    //商品业绩，数量，提成
                    if ($v['type'] == '2') {
                        $result['data'][$key]['goods_achievement'] += $v['money'];
                        $total['goods_achievement'] += $v['money'];
                        $result['data'][$key]['goods_commission'] += $v['commission'];
                        $total['goods_commission'] += $v['commission'];
                        $result['data'][$key]['total_commission'] += $v['commission'];
                        $total['total_commission'] += $v['commission'];
                        $result['data'][$key]['total_achievement'] += $v['money'];
                        $total['total_achievement'] += $v['money'];
                        $result['data'][$key]['goods'][$i] = $v['project_id'];
                        $result['data'][$key]['goods_num'][$v['project_id']] += $v['goods_num'];
                    }
                    //售卡业绩，数量，提成
                    if ($v['type'] == '3' || $v['type'] == '4'  || $v['type'] == '5' || $v['type'] == '7') {
                        $result['data'][$key]['card_achievement'] += $v['money'];
                        $total['card_achievement'] += $v['money'];
                        $result['data'][$key]['card_number'] += 1;
                        $total['card_number'] += 1;
                        $result['data'][$key]['card_commission'] += $v['commission'];
                        $total['card_commission'] += $v['commission'];
                        $result['data'][$key]['total_commission'] += $v['commission'];
                        $total['total_commission'] += $v['commission'];
                        $result['data'][$key]['total_achievement'] += $v['money'];
                        $total['total_achievement'] += $v['money'];
                        $result['data'][$key]['card'][$i] = $v['order_project_id'];

                    }
                    $i++;
                }
                //处理项目
                if (!empty($result['data'][$key]['project'])) {
                    $result['data'][$key]['project'] = array_count_values($result['data'][$key]['project']);
                    $projectString = '';
                    foreach ($result['data'][$key]['project'] as $k => $v) {
                        $name = Db::name('qt_project')->where('id', $k)->value('name');
                        $projectString .= $name . '(' . $v . ')，';
                    }
                    $result['data'][$key]['project'] = rtrim($projectString, "，");
                } else {
                    $result['data'][$key]['project'] = '';
                }
                //处理商品
                if (!empty($result['data'][$key]['goods'])) {
                    $result['data'][$key]['goods'] = array_count_values($result['data'][$key]['goods']);
                    $goodsString = '';
                    foreach ($result['data'][$key]['goods'] as $k => $v) {
                        $name = Db::name('qt_goods')->where('id', $k)->value('name');
                        $goodsString .= $name . '(' . $result['data'][$key]['goods_num'][$k] . ')，';
                    }
                    $result['data'][$key]['goods'] = rtrim($goodsString, "，");
                } else {
                    $result['data'][$key]['goods'] = '';
                }
                if (!empty($result['data'][$key]['card'])) {
                    //处理售卡
                    $cardRecharge = Db::name('qt_viper_card_recharge_log')
                        ->whereIn('id',$result['data'][$key]['card'])
                        ->group('recharge_tempid')
                        ->column('recharge_temp_name,count(recharge_tempid) as number');
                    $cardString = "";
                    foreach ($cardRecharge as $k=>$v){
                        $cardString.= $k.'('.$v.')，';
                    }
                    $result['data'][$key]['card'] = rtrim($cardString, "，");

                } else {
                    $result['data'][$key]['card'] = '';
                }
            }
            //处理小数点问题
            foreach ($total as $key => $value) {
                $total[$key] = ceil($value);
            }
            $result['total'] = $total;
            $this->result('查询成功', $result, 200);
        }
    }


    /**
     * 技师业绩报表
     */
    public function receipts()
    {
        if ($this->request->isPost()) {
            $getData = $this->request->post();
            $userInfo = $this->userInfo;
            if (!empty($getData['type'] == '1')) {
                $field = 'cl.created_time';
            } else {
                $field = 'cl.updated_time';
            }
            if (empty($getData['start_time']) || empty($getData['end_time'])) {
                $this->result('缺少时间类型', '', 204);
            }
            $sql = Db::name('qt_commission_log')
                ->alias('cl')
                ->leftJoin('qt_users u', 'u.id = cl.user_id')
                ->where(['cl.store_id' => $userInfo['store_id']])
                ->whereBetweenTime($field, strtotime($getData['start_time']),strtotime($getData['end_time']));
            //技师筛选
            if (!empty($getData['engineer_id'])) {
                $sql->where('cl.user_id', $getData['engineer_id']);
            }
            if (!empty($getData['work_type'])) {
                //获取工种所有的用户
                $workType = Db::name('qt_work_type')->where('id', $getData['work_type'])->find();
                if ($workType['pid'] == '0') {
                    //顶级分类查询所有的下级
                    $workRes = Db::name('qt_work_type')->where('pid', $getData['work_type'])->column('id');
                    $workTypeWhere[0] = $workType['id'];
                    $workRes = array_merge($workRes, $workTypeWhere);
                    $users = Db::name('qt_users')->whereIn('top_type_id', $workRes)->column('id');
                    $sql->whereIn('user_id', $users);
                } else {
                    $users = Db::name('qt_users')->where('top_type_id', $getData['work_type'])->column('id');
                    $sql->whereIn('user_id', $users);
                }
            }
            //用户ID合集
            $userIds = $sql->group('cl.user_id')->where('cl.user_id', 'neq', 0)->where('cl.payment_price','neq','0')->column('user_id');
            $result = [];
            $result['data'] = [];
            $total = [
                'project_goods_achievement' => 0,//项目+商品业绩
                'project_goods_payment_price' => 0,//项目+商品实收业绩
                'project_number' => 0,//项目数量
                'project_goods_commission' => 0,//项目+商品提成
                'card_achievement' => 0,//售卡业绩
                'card_number' => 0,//售卡总数
                'card_commission' => 0,//售卡提成
                'tuijian' => 0,//推荐提成
                'total_commission' => 0,//总提成
                'total_achievement' => 0,//总业绩
            ];
            foreach ($userIds as $key => $value) {
                $user = Db::name('qt_users')->field('id,work_number,real_name')->where('id', $value)->find();
                $result['data'][$key] = [
                    'id' => $user['id'],
                    'real_name' => $user['real_name'],
                    'work_number' => $user['work_number'],
                    'project_goods_achievement' => 0,//项目+商品业绩
                    'project_goods_payment_price' => 0,//项目+商品实收业绩
                    'project_number' => 0,//项目数量
                    'project_goods_commission' => 0,//项目+商品提成
                    'card_achievement' => 0,//售卡业绩
                    'card_number' => 0,//售卡总数
                    'card_commission' => 0,//售卡提成
                    'tuijian' => 0,//推荐提成
                    'total_commission' => 0,//总提成
                    'total_achievement' => 0,//总业绩
                ];
                static $product = [];
                $i = 0;
                //查询当前用户所有的数据
                $data = Db::name('qt_commission_log')
                    ->field('cl.product_id as project_id,p.name as project_name,cl.status,cl.is_add,cl.commission,cl.money,cl.type,cl.payment_price,cl.is_first')
                    ->alias('cl')
                    ->leftJoin('qt_project p', 'p.id = cl.product_id')
                    ->where(['cl.store_id' => $userInfo['store_id']])
                    ->whereBetweenTime($field, $getData['start_time'], $getData['end_time'])
                    ->where(['cl.user_id' => $user['id']])
                    ->select();
                if (!empty($data)) {
                    foreach ($data as $k => $v) {
                        //项目业绩，数量，提成
                        if ($v['type'] == '1' || $v['type'] == '6'  || $v['type'] == '2') {
                            $result['data'][$key]['project_goods_achievement'] += $v['money'];
                            $result['data'][$key]['project_goods_payment_price'] += $v['payment_price'];
                            $total['project_goods_achievement'] += $v['money'];
                            $total['project_goods_payment_price'] += $v['payment_price'];
                            if($v['type'] == '1'){
                                $result['data'][$key]['project_number'] += 1;
                                $total['project_number'] += 1;
                            }
                            $result['data'][$key]['project_goods_commission'] += $v['commission'];
                            $total['project_goods_commission'] += $v['commission'];

                            $result['data'][$key]['total_commission'] += $v['commission'];
                            $total['total_commission'] += $v['commission'];

                            $result['data'][$key]['total_achievement'] += $v['money'];
                            $total['total_achievement'] += $v['money'];
                        }
                        //售卡业绩，数量，提成
                        if ($v['type'] == '3' || $v['type'] == '4' || $v['type'] == '5' || $v['type'] == '7') {
                            $result['data'][$key]['card_achievement'] += $v['money'];
                            $total['card_achievement'] += $v['money'];
                            $result['data'][$key]['card_number'] += 1;
                            $total['card_number'] += 1;
                            $result['data'][$key]['card_commission'] += $v['commission'];
                            $total['card_commission'] += $v['commission'];
                            $result['data'][$key]['total_commission'] += $v['commission'];
                            $total['total_commission'] += $v['commission'];
                            $result['data'][$key]['total_achievement'] += $v['money'];
                            $total['total_achievement'] += $v['money'];
                            $result['data'][$key]['card'][$i] = $v['project_id'];
                        }
                        $i++;
                    }
                } else {
                    $result['data'] = [];
                }
            }
            $result['data'] = array_values($result['data']);
            //处理小数点问题
            foreach ($total as $key => $value) {
                $total[$key] = ceil($value);
            }
            $result['total'] = $total;
            $this->result('查询成功', $result, 200);
        }
    }


    /**
     * 楼面业绩明细报表
     */
    public function floorReceiptsDetail()
    {
        if ($this->request->isPost()) {
            $getData = $this->request->post();
            $userInfo = $this->userInfo;
            if (!empty($getData['type'] == '1')) {
                $field = 'log.created_time';
            } else {
                $field = 'log.updated_time';
            }
            if (empty($getData['start_time']) || empty($getData['end_time'])) {
                $this->result('缺少时间类型', '', 204);
            }
            $sql = Db::name('qt_commission_log')
                ->alias('log')
                ->leftJoin('qt_users u', 'u.id = log.cashier_id')
                ->where(['log.store_id' => $userInfo['store_id']])
                ->where('log.cashier_id','neq','0')
                ->whereBetweenTime($field, $getData['start_time'], $getData['end_time']);
            if (empty($getData['limit'])) {
                $getData['limit'] = '10';
            }
            //技师号筛选
            if (!empty($getData['work_number'])) {
                $sql->where('u.work_number', $getData['work_number']);
            }
            //技师工种筛选
            if (!empty($getData['top_type_id'])) {
                $sql->where('u.top_type_id', $getData['top_type_id']);
            }
            $userIds = $sql->group('log.cashier_id')
                //->where('log.cashier_id', 'neq', 0)
                ->column('log.cashier_id');
            $result = [];
            $total = [
                'project_achievement' => 0,//项目业绩
                'project_number' => 0,//项目数量
                'project_commission' => 0,//项目提成
                'goods_achievement' => 0,//商品业绩
                'goods_commission' => 0,//商品提成
                'card_achievement' => 0,//售卡业绩
                'card_number' => 0,//售卡总数
                'card_commission' => 0,//售卡提成
                'tuijian' => 0,//推荐提成
                'total_commission' => 0,//总提成
                'total_payment_achievement' => 0,//总业绩
                'total_achievement' => 0,//总业绩
                'total_all' => 0,//排钟
                'total_spot' => 0,//点钟
                'total_call' => 0,//call钟
                'total_add' => 0,//加钟
                'total_add_work' => 0,//加班
                'total_adjunct' => 0,//副项
                'total_main' => 0,//主项合计
                'total_adjunct_project' => 0,//副项合计
                'total_project' => 0,//总合计
            ];
            $result['data'] = [];
            foreach ($userIds as $key => $value) {
                $user = Db::name('qt_users')->field('id,work_number,real_name,group')->where('id', $value)->find();
                if($user['group'] == '1'){
                    unset($user[$key]);
                    continue;
                }
                $result['data'][$key] = [
                    'id' => $user['id'],
                    'real_name' => $user['real_name'],
                    'work_number' => $user['work_number'],
                    'project_achievement' => 0,//项目业绩
                    'project_number' => 0,//项目数量
                    'project_commission' => 0,//项目提成
                    'goods_achievement' => 0,//商品业绩
                    'goods_commission' => 0,//商品提成
                    'card_achievement' => 0,//售卡业绩
                    'card_number' => 0,//售卡总数
                    'card_commission' => 0,//售卡提成
                    'tuijian' => 0,//推荐提成
                    'total_commission' => 0,//总提成
                    'total_payment_achievement' => 0,//总提成
                    'total_achievement' => 0,//总业绩
                    'total_all' => 0,//排钟
                    'total_spot' => 0,//点钟
                    'total_call' => 0,//call钟
                    'total_add' => 0,//加钟
                    'total_add_work' => 0,//加班
                    'total_adjunct' => 0,//副项
                    'total_main' => 0,//主项合计
                    'total_adjunct_project' => 0,//副项合计
                    'total_project' => 0,//总合计
                    'goods_num'=>[]
                ];
                static $product = [];
                $i = 0;
                $j = 0;
                //查询当前用户所有的数据
                $data = Db::name('qt_commission_log')
                    ->field('log.product_id as project_id,p.name as project_name,log.status,log.is_add,log.commission,log.payment_price as money,log.type,log.receiver,item.goods_num,log.is_first,log.order_project_id,log.payment_price as  payment_money')
                    ->alias('log')
                    ->leftJoin('qt_project p', 'p.id = log.product_id')
                    ->leftJoin('qt_product_order_item item', 'log.order_project_id = item.id')
                    ->where(['log.store_id' => $userInfo['store_id']])
                    ->whereBetweenTime($field, $getData['start_time'], $getData['end_time'])
                    ->where(['log.cashier_id' => $user['id']])
                    ->select();
                foreach ($data as $k => $v) {
                    $result['data'][$key]['goods_num'][$v['project_id']] = 0;
                    $total['total_payment_achievement'] += $value['payment_money'];
                    if ($v['type'] == '3' || $v['type'] == '4' || $v['type'] == '5' || $v['type'] == '7') {
                        $result['data'][$key]['total_payment_achievement'] += $v['money'];
                    }else{
                        $result['data'][$key]['total_payment_achievement'] += $v['payment_money'];
                    }
                    $result['data'][$key]['total_payment_achievement'] +=  $value['payment_money'];
                    //项目业绩，数量，提成
                    if ($v['type'] == '1' || $v['type'] == '6') {
                        $result['data'][$key]['project_achievement'] += $v['money'];

                        $total['project_achievement'] += $v['money'];
                        $result['data'][$key]['project_number'] += 1;
                        $total['project_number'] += 1;
                        $result['data'][$key]['project_commission'] += $v['receiver'];
                        $total['project_commission'] += $v['receiver'];
                        $result['data'][$key]['total_commission'] += $v['receiver'];
                        $total['total_commission'] += $v['receiver'];
                        $result['data'][$key]['total_achievement'] += $v['money'];
                        $total['total_achievement'] += $v['money'];
                        $result['data'][$key]['project'][$i] = $v['project_id'];
                        //处理主副项合计
                        if ($v['status'] != '6') {
                            $result['data'][$key]['total_project'] += 1;
                            $total['total_project'] += 1;
                        } else {
                            $result['data'][$key]['total_adjunct'] += 1;
                            $total['total_adjunct'] += 1;
                            $result['data'][$key]['total_adjunct_project'] += 1;
                            $total['total_adjunct_project'] += 1;
                        }
                        //所有项目合计
                        $result['data'][$key]['total_project'] += 1;
                        $total['total_project'] += 1;
                        //轮排
                        if ($v['status'] == '1' && $v['is_add'] == '1') {
                            $result['data'][$key]['total_all'] += 1;
                            $total['total_all'] += 1;
                        }
                        //call钟
                        if ($v['status'] == '3' && $v['is_add'] == '1') {
                            $result['data'][$key]['total_call'] += 1;
                            $total['total_call'] += 1;
                        }

                        //点钟
                        if ($v['status'] == '2' && $v['is_add'] == '1') {
                            $result['data'][$key]['total_spot'] += 1;
                            $total['total_spot'] += 1;
                        }
                        //加钟
                        if ($v['status'] == '5' && $v['is_add'] == '1') {
                            $result['data'][$key]['total_add'] += 1;
                            $total['total_add'] += 1;
                        }
                        //加班
                        if ($v['status'] == '1' && $v['is_add'] == '2') {
                            $result['data'][$key]['total_add_work'] += 1;
                            $total['total_add_work'] += 1;
                        }
                        if ($v['status'] == '4' && $v['is_add'] == '2') {
                            $result['data'][$key]['total_add_work'] += 1;
                            $total['total_add_work'] += 1;
                        }
                        if ($v['status'] == '2' && $v['is_add'] == '2') {
                            $result['data'][$key]['total_add_work'] += 1;
                            $total['total_add_work'] += 1;
                        }
                    }
                    //商品业绩，数量，提成
                    if ($v['type'] == '2') {
                        $result['data'][$key]['goods_achievement'] += $v['money'];
                        $total['goods_achievement'] += $v['money'];
                        $result['data'][$key]['goods_commission'] += $v['receiver'];
                        $total['goods_commission'] += $v['receiver'];
                        $result['data'][$key]['total_commission'] += $v['receiver'];
                        $total['total_commission'] += $v['receiver'];
                        $result['data'][$key]['total_achievement'] += $v['money'];
                        $total['total_achievement'] += $v['money'];
                        $result['data'][$key]['goods'][$i] = $v['project_id'];
                        $result['data'][$key]['goods_num'][$v['project_id']] += $v['goods_num'];
                    }
                    //售卡业绩，数量，提成
                    if ($v['type'] == '3' || $v['type'] == '4' || $v['type'] == '5' || $v['type'] == '7') {
                        $result['data'][$key]['card_achievement'] += $v['money'];
                        $total['card_achievement'] += $v['money'];
                        $result['data'][$key]['card_number'] += 1;
                        $total['card_number'] += 1;
                        $result['data'][$key]['card_commission'] += $v['receiver'];
                        $total['card_commission'] += $v['receiver'];
                        $result['data'][$key]['total_commission'] += $v['receiver'];
                        $total['total_commission'] += $v['receiver'];
                        $result['data'][$key]['total_achievement'] += $v['money'];
                        $total['total_achievement'] += $v['money'];
                        $result['data'][$key]['card'][$i] = $v['order_project_id'];
                    }
                    $i++;
                    $j++;
                }
                //处理项目
                if (!empty($result['data'][$key]['project'])) {
                    $result['data'][$key]['project'] = array_count_values($result['data'][$key]['project']);
                    $projectString = '';
                    foreach ($result['data'][$key]['project'] as $k => $v) {
                        $name = Db::name('qt_project')->where('id', $k)->value('name');
                        $projectString .= $name . '(' . $v . ')，';
                    }
                    $result['data'][$key]['project'] = rtrim($projectString, "，");
                } else {
                    $result['data'][$key]['project'] = '';
                }

                //处理商品
                $goodsString = '';

                if (!empty($result['data'][$key]['goods'])) {
                    $result['data'][$key]['goods'] = array_count_values($result['data'][$key]['goods']);
                    foreach ($result['data'][$key]['goods_num'] as $k => $v) {
//                        if($v == 0){
//                            unset($result['data'][$key]['goods_num'][$k]);
//                        }
                    }
                    foreach ($result['data'][$key]['goods'] as $k => $v) {
                        $name = Db::name('qt_goods')->where('id', $k)->value('name');
                        $goodsString .= $name . '(' . $result['data'][$key]['goods_num'][$k] . ')，';
                    }
                    $result['data'][$key]['goods'] = rtrim($goodsString, "，");
                } else {
                    $result['data'][$key]['goods'] = '';
                }
                if (!empty($result['data'][$key]['card'])) {
                    $cardRecharge = Db::name('qt_viper_card_recharge_log')
                        ->whereIn('id',$result['data'][$key]['card'])
                        ->group('recharge_tempid')
                        ->column('recharge_temp_name,count(recharge_tempid) as number');
                    $cardString = "";
                    foreach ($cardRecharge as $k=>$v){
                        $cardString.= $k.'('.$v.')，';
                    }
                    $result['data'][$key]['card'] = rtrim($cardString, "，");
                } else {
                    $result['data'][$key]['card'] = '';
                }
            }
            $result['data'] = array_values($result['data']);
            //处理小数点问题
            foreach ($total as $key => $value) {
                $total[$key] = ceil($value);
            }
            $result['total'] = $total;
            $this->result('查询成功', $result, 200);
        }
    }


    /**
     * 楼面业绩报表
     */
    public function floorReceipts()
    {
        if ($this->request->isPost()) {
            $getData = $this->request->post();
            $userInfo = $this->userInfo;
            if (!empty($getData['type'] == '1')) {
                $field = 'created_time';
            } else {
                $field = 'updated_time';
            }
            if (empty($getData['start_time']) || empty($getData['end_time'])) {
                $this->result('缺少时间类型', '', 204);
            }
            $sql = Db::name('qt_commission_log')
                ->where(['store_id' => $userInfo['store_id']])
                ->whereBetweenTime($field, $getData['start_time'], $getData['end_time']);
            if (!empty($getData['cashier_id'])) {
                $sql->where('cashier_id', $getData['cashier_id']);
            }
            //用户ID合集
            $userOrderData = $sql->group('cashier_id')
                ->where('cashier_id', 'neq', '0')
                ->order('receiver desc')
                ->column('cashier_id,sum(receiver)');
            foreach ($userOrderData as $key => $value) {
                $userIds[] = $key;
            }
            $result = [];
            $total = [
                'project_goods_achievement' => 0,//项目+商品业绩
                'project_goods_payment_price' => 0,//项目+商品实收业绩
                'project_number' => 0,//项目数量
                'project_goods_commission' => 0,//项目+商品提成
                'card_achievement' => 0,//售卡业绩
                'card_number' => 0,//售卡总数
                'card_commission' => 0,//售卡提成
                'tuijian' => 0,//推荐提成
                'total_commission' => 0,//总提成
                'total_achievement' => 0,//总业绩
            ];
            if (!empty($userIds)) {
                foreach ($userIds as $key => $value) {
                    $user = Db::name('qt_users')->field('id,work_number,real_name,group')->where('id', $value)->find();
                    if($user['group'] == '1'){
                        unset($userIds[$key]);
                        continue;
                    }
                    $result['data'][$key] = [
                        'id' => $user['id'],
                        'real_name' => $user['real_name'],
                        'work_number' => $user['work_number'],
                        'project_goods_achievement' => 0,//项目+商品业绩
                        'project_goods_payment_price' => 0,//项目+商品实收业绩
                        'project_number' => 0,//项目数量
                        'project_goods_commission' => 0,//项目+商品提成
                        'card_achievement' => 0,//售卡业绩
                        'card_number' => 0,//售卡总数
                        'card_commission' => 0,//售卡提成
                        'tuijian' => 0,//推荐提成
                        'total_commission' => 0,//总提成
                        'total_achievement' => 0,//总业绩
                        'total_payment_achievement' => 0,//总实收业绩
                    ];
                    static $product = [];
                    $i = 0;
                    //查询当前用户所有的数据
                    $data = Db::name('qt_commission_log')
                        ->field('cl.product_id as project_id,p.name as project_name,cl.status,cl.is_add,cl.commission,cl.money,cl.type,cl.receiver,cl.payment_price')
                        ->alias('cl')
                        ->leftJoin('qt_project p', 'p.id = cl.product_id')
                        ->where(['cl.store_id' => $userInfo['store_id']])
                        ->whereBetweenTime($field, $getData['start_time'], $getData['end_time'])
                        ->where(['cl.cashier_id' => $user['id']])
                        ->select();
                    if (!empty($data)) {
                        foreach ($data as $k => $v) {
                            //项目业绩，数量，提成
                            if ($v['type'] == '1' || $v['type'] == '6') {
                                $result['data'][$key]['project_goods_achievement'] += $v['money'];
                                $result['data'][$key]['project_goods_payment_price'] += $v['payment_price'];
                                $result['data'][$key]['total_payment_achievement'] += $v['payment_price'];
                                $total['project_goods_achievement'] += $v['money'];
                                $total['project_goods_payment_price'] += $v['payment_price'];
                                $result['data'][$key]['project_number'] += 1;
                                $total['project_number'] += 1;
                                $result['data'][$key]['project_goods_commission'] += $v['receiver'];
                                $total['project_goods_commission'] += $v['receiver'];
                                $result['data'][$key]['total_commission'] += $v['receiver'];
                                $total['total_commission'] += $v['receiver'];
                                $result['data'][$key]['total_achievement'] += $v['money'];
                                $total['total_achievement'] += $v['money'];
                            }
                            //商品业绩，数量，提成
                            if ($v['type'] == '2') {
                                $result['data'][$key]['project_goods_achievement'] += $v['money'];
                                $result['data'][$key]['project_goods_payment_price'] += $v['payment_price'];
                                $total['project_goods_achievement'] += $v['money'];
                                $total['project_goods_payment_price'] += $v['payment_price'];
                                $result['data'][$key]['project_goods_commission'] += $v['receiver'];
                                $total['project_goods_commission'] += $v['receiver'];
                                $result['data'][$key]['total_payment_achievement'] += $v['payment_price'];
                                $result['data'][$key]['total_commission'] += $v['receiver'];
                                $total['total_commission'] += $v['receiver'];

                                $result['data'][$key]['total_achievement'] += $v['money'];
                                $total['total_achievement'] += $v['money'];
                            }
                            //售卡业绩，数量，提成
                            if ($v['type'] == '3' || $v['type'] == '4' || $v['type'] == '5' || $v['type'] == '7') {
                                $result['data'][$key]['card_achievement'] += $v['money'];
                                $result['data'][$key]['total_payment_achievement'] += $v['money'];
                                $total['card_achievement'] += $v['money'];
                                $result['data'][$key]['card_number'] += 1;
                                $total['card_number'] += 1;
                                $result['data'][$key]['card_commission'] += $v['receiver'];
                                $total['card_commission'] += $v['receiver'];
                                $result['data'][$key]['total_commission'] += $v['receiver'];
                                $total['total_commission'] += $v['receiver'];
                                $result['data'][$key]['total_achievement'] += $v['money'];
                                $total['total_achievement'] += $v['money'];
                            }
                            $i++;
                        }
                    }
                }
                //处理小数点问题
                foreach ($total as $key => $value) {
                    $total[$key] = ceil($value);
                }
            } else {
                $result['data'] = [];
            }
            $result['data'] = array_values($result['data']);
            $result['total'] = $total;
            $this->result('查询成功', $result, 200);
        }
    }


    /**
     * 消费汇总报表
     */
    public function consumptionSummary()
    {
        if ($this->request->isPost()) {
            $getData = $this->request->post();
            $userInfo = $this->userInfo;
            if (!empty($getData['type'] == '1')) {
                $field = 'created_time';
            } else {
                $field = 'updated_time';
            }
            if (empty($getData['start_time']) || empty($getData['end_time'])) {
                $this->result('缺少时间类型', '', 204);
            }
            $sql = Db::name('qt_commission_log')
                ->alias('cl')
                ->leftJoin('qt_project p', 'p.id = cl.product_id ')
                ->field('cl.product_id,count(cl.id) as number,sum(cl.payment_price) as money,p.name,p.project_category_id as cate_id')
                ->where(['cl.store_id' => $userInfo['store_id']])
                ->where('cl.updated_time','neq','0')
                ->whereBetweenTime($field, $getData['start_time'], $getData['end_time'])
                ->whereIn('cl.type', ['1', '6']);
            //项目分类筛选
            if (!empty($getData['project_type'])) {
                $sql->whereIn('p.project_category_id', $getData['project_type']);
            }
            $project = $sql->group('product_id')->select();
            //查询所有的项目分类
            $userInfo = $this->userInfo;
            static $tree = [];
            $data = Db::name('qt_project_category')
                ->field('id,name')
                ->where(['store_id' => $userInfo['store_id'], 'pid' => '0'])
                ->order('sort asc')
                ->select();
            foreach ($data as $key => $value) {
                $tree[] = $value;
                $child = Db::name('qt_project_category')
                    ->where(['store_id' => $this->userInfo['store_id'], 'pid' => $value['id']])
                    ->field('id,name')
                    ->select();
                foreach ($child as $k => $val) {
                    $tree[] = $val;
                }

            }
            $projectCate = $tree;

            $result['project'] = [];
            foreach ($projectCate as $key => $value) {
                $result['project'][$value['id']]['total_number'] = 0;
                $result['project'][$value['id']]['total_money'] = 0;
                $result['project'][$value['id']]['id'] = $value['id'];
                $result['project'][$value['id']]['name'] = $value['name'];
                $result['project'][$value['id']]['project'] = [];
                $result['project'][$value['id']]['product_type'] = '1';
            }
            //循环插入数据
            foreach ($project as $key => $value) {
                $result['project'][$value['cate_id']]['project'][] = $value;
                $result['project'][$value['cate_id']]['total_number'] += $value['number'];
                $result['project'][$value['cate_id']]['total_money'] += $value['money'];
            }
            //处理没有项目分类
            foreach ($result['project'] as $key => $value) {
                if (empty($value['project'])) {
                    unset($result['project'][$key]);
                }
            }
            $result['project'] = array_values($result['project']);
            //处理商品
            $sql = Db::name('qt_commission_log')
                ->alias('cl')
                ->leftJoin('qt_goods g', 'g.id = cl.product_id ')
                ->leftJoin('qt_product_order_item item', 'cl.order_project_id = item.id ')
                ->field('cl.product_id,sum(item.goods_num) as number,sum(cl.money) as money,g.name,g.goods_category_id as cate_id')
                ->where(['cl.store_id' => $userInfo['store_id'],'item.status'=>'1'])
                ->whereBetweenTime($field, $getData['start_time'], $getData['end_time'])
                ->whereIn('cl.type', ['2']);
            //查询所有的项目分类
            //商品分类
            $goodsCate = Db::name('qt_goods_category')
                ->field('id,name')
                ->where(['store_id' => $userInfo['store_id']])
                ->order('sort asc')
                ->select();
            //项目分类筛选
            if (!empty($getData['goods_type'])) {
                $goodsCate = Db::name('qt_goods_category')
                    ->field('id,name')
                    ->where(['store_id' => $userInfo['store_id'], 'id' => $getData['goods_type']])
                    ->order('sort asc')
                    ->select();
                $goodsCateWhere = Db::name('qt_goods_category')
                    ->field('id,name')
                    ->where(['store_id' => $userInfo['store_id'], 'id' => $getData['goods_type']])
                    ->order('sort asc')
                    ->column('id');
                $sql->whereIn('g.goods_category_id', $goodsCateWhere);
            }
            $goods = $sql->group('product_id')->select();
            $result['goods'] = [];
            foreach ($goodsCate as $key => $value) {
                $result['goods'][$value['id']]['total_number'] = 0;
                $result['goods'][$value['id']]['total_money'] = 0;
                $result['goods'][$value['id']]['id'] = $value['id'];
                $result['goods'][$value['id']]['name'] = $value['name'];
                $result['goods'][$value['id']]['goods'] = [];
                $result['goods'][$value['id']]['total_number'] = 0;
                $result['goods'][$value['id']]['total_money'] = 0;
                $result['goods'][$value['id']]['total_money'] = 0;
                $result['goods'][$value['id']]['product_type'] = 2;
            }
            //循环插入数据
            foreach ($goods as $key => $value) {
                $result['goods'][$value['cate_id']]['goods'][] = $value;
                $result['goods'][$value['cate_id']]['total_number'] += $value['number'];
                $result['goods'][$value['cate_id']]['total_money'] += $value['money'];
            }
            //处理没有项目分类
            foreach ($result['goods'] as $key => $value) {
                if (empty($value['goods'])) {
                    unset($result['goods'][$key]);
                }
            }
            $result['goods'] = array_values($result['goods']);
            $this->result('查询成功', $result, 200);
        }
        $this->result('无效的请求', '', 204);
    }


    /**
     * 消费汇总报表单个详情
     */
    public function consumptionSummaryDetail()
    {
        if ($this->request->isPost()) {
            $getData = $this->request->post();
            $userInfo = $this->userInfo;
            if (!empty($getData['type'] == '1')) {
                $field = 'created_time';
            } else {
                $field = 'updated_time';
            }
            if (empty($getData['start_time']) || empty($getData['end_time'])) {
                $this->result('缺少时间类型', '', 204);
            }
            //处理商品
            $sql = Db::name('qt_commission_log')
                ->alias('cl')
                ->leftJoin('qt_goods g', 'g.id = cl.product_id ')
                ->leftJoin('qt_product_order_item po', 'po.id = cl.order_project_id ')
                ->field([
                    'cl.product_id,
                    count(cl.id) as total_number,
                    sum(cl.money) as total_money,
                    g.name,
                    cl.order_project_id'
                ])
                ->where(['cl.store_id' => $userInfo['store_id'], 'cl.product_id' => $getData['product_id']])
                ->whereBetweenTime($field, $getData['start_time'], $getData['end_time'])
                ->whereIn('cl.type', ['2']);
            $data = $sql->find();
            if ($getData['product_type'] == '1') {
                $list = Db::name('qt_order_project')
                    ->field('oi.name as product_name,r.name as room_name,oi.created_time,oi.num as number,h.name as hand_card_name,oi.order_type,oi.start_time,oi.end_time,u.work_number')
                    ->alias('oi')
                    ->leftJoin('qt_room r', 'r.id = oi.room_id')
                    ->leftJoin('qt_hand_card h', 'h.id = oi.hand_card_id')
                    ->leftJoin('qt_users u', 'u.id = oi.engineer_id')
                    ->where(['oi.project_id' => $getData['product_id']])
                    ->where(['oi.is_change' => '1','pay_status'=>'1'])
                    ->paginate($getData['limit']);
            } else {
                $list = Db::name('qt_product_order_item')
                    ->field('oi.goods_name as product_name,r.name as room_name,oi.create_time,oi.goods_num as number,h.name as hand_card_name')
                    ->alias('oi')
                    ->leftJoin('qt_room r', 'r.id = oi.room_id')
                    ->leftJoin('qt_hand_card h', 'h.id = oi.hand_card_id')
                    ->where(['oi.id' => $data['order_project_id']])
                    ->paginate($getData['limit'])->each(function ($item) {
                        $item['order_type'] = '';
                        $item['work_number'] = '';
                        $item['start_time'] = null;
                        $item['end_time'] = null;
                        return $item;
                    });
            }

            $data['list'] = $list;
            $this->result('查询成功', $data, 200);
        }
        $this->result('无效的请求', '', 204);
    }


    /**
     * 项目汇总报表
     */
    public function projectTotal()
    {
        if ($this->request->isPost()) {
            $getData = $this->request->post();
            $userInfo = $this->userInfo;
            if (!empty($getData['type'] == '1')) {
                $field = 'created_time';
            } else {
                $field = 'updated_time';
            }
            if (empty($getData['start_time']) || empty($getData['end_time'])) {
                $this->result('缺少时间类型', '', 204);
            }
//            $sql = Db::name('qt_commission_log')
//                ->alias('cl')
//                ->leftJoin('qt_project p', 'p.id = cl.product_id ')
//                ->field('cl.product_id,count(cl.id) as number,sum(cl.payment_price) as money,p.name,p.project_category_id as cate_id')
//                ->where(['cl.store_id' => $userInfo['store_id']])
//                ->whereBetweenTime($field, $getData['start_time'], $getData['end_time'])
//                ->whereIn('cl.type', ['1', '6']);

            $sql = Db::name('qt_project')->field('id,name')
                ->where(['store_id' => $this->userInfo['store_id']]);
            //项目筛选
            if (!empty($getData['project_id'])) {
                $sql->where('id', $getData['project_id']);
            }
            //项目类型筛选
            if (!empty($getData['project_type'])) {
                $sql->where('project_category_id', $getData['project_type']);
            }
            $result = $sql->paginate($getData['limit'])
                ->each(function ($item) use ($field, $getData, $userInfo) {
                //初始化数据
                $item['project_name'] = $item['name'];
                $item['spot'] = 0;//点钟
                $item['add_spot'] = 0;//加班点钟
                $item['total_spot'] = 0;//点钟合计
                $item['row'] = 0;//排钟
                $item['add_row'] = 0;//加班排钟
                $item['chose'] = 0;//选钟
                $item['add_chose'] = 0;//加班选钟
                $item['total_row'] = 0;//排钟合计
                $item['call'] = 0;//call钟
                $item['add'] = 0;//加钟
                $item['number'] = 0;//总数量
                $item['money'] = 0;//总数量
                //查询当前用户所有的项目
                $data = Db::name('qt_commission_log')
                    ->field('cl.product_id ,p.name as project_name,cl.status,cl.is_add,cl.commission,cl.money,cl.type')
                    ->alias('cl')
                    ->leftJoin('qt_project p', 'p.id = cl.product_id')
                    ->where(['cl.store_id' => $userInfo['store_id']])
                    ->whereBetweenTime($field, $getData['start_time'], $getData['end_time'])
                    ->whereIn('cl.type', ['1', '6'])->select();
                foreach ($data as $k => $v) {
                    if ($v['product_id'] == $item['id']) {
                        $item['number'] += 1;
                        $item['money'] += $v['money'];
                        //点钟数量
                        if ($v['status'] == '2' && $v['is_add'] == '1') {
                            $item['spot'] += 1;
                            $item['total_spot'] += 1;//点钟合计
                        }
                        //加班点钟数量
                        if ($v['status'] == '2' && $v['is_add'] == '2') {
                            $item['add_spot'] += 1;
                            $item['total_spot'] += 1;//点钟合计
                        }
                        //排钟数量
                        if ($v['status'] == '1' && $v['is_add'] == '1') {
                            $item['row'] += 1;
                            $item['total_row'] += 1;
                        }
                        //加班排钟数量
                        if ($v['status'] == '1' && $v['is_add'] == '2') {
                            $item['add_row'] += 1;
                            $item['total_row'] += 1;//排钟合计
                        }
                        //选钟数量
                        if ($v['status'] == '4' && $v['is_add'] == '1') {
                            $item['chose'] += 1;
                            $item['total_row'] += 1;//排钟合计
                        }
                        //加班选钟数量
                        if ($v['status'] == '4' && $v['is_add'] == '2') {
                            $item['add_chose'] += 1;
                            $item['total_row'] += 1;//排钟合计
                        }
                        //call钟数量
                        if ($v['status'] == '3') {
                            $item['call'] += 1;
                        }
                        //加钟数量
                        if ($v['status'] == '5') {
                            $item['add'] += 1;
                        }
                    }

                }
                return $item;
            });
            $this->result('查询成功', $result, 200);
        }
    }


    /**
     * 项目汇总简明报表
     */
    public function simpleProjectTotal()
    {
        if ($this->request->isPost()) {
            $getData = $this->request->post();
            $userInfo = $this->userInfo;
            if (!empty($getData['type'] == '1')) {
                $field = 'created_time';
            } else {
                $field = 'updated_time';
            }
            if (empty($getData['start_time']) || empty($getData['end_time'])) {
                $this->result('缺少时间类型', '', 204);
            }
            $sql = Db::name('qt_commission_log')
                ->alias('cl')
                ->leftJoin('qt_project p', 'p.id = cl.product_id ')
                ->field('cl.product_id,count(cl.id) as number,sum(cl.money) as money,p.name,p.project_category_id as cate_id')
                ->where(['cl.store_id' => $userInfo['store_id']])
                ->whereBetweenTime($field, $getData['start_time'], $getData['end_time'])
                ->whereIn('cl.type', ['1', '6']);
            if (!empty($getData['project_type'])) {
                $sql->where('p.project_category_id', $getData['project_type']);
            }
            $project = $sql->group('product_id')->select();
            //查询当前用户所有的项目
            $data = Db::name('qt_commission_log')
                ->field('cl.product_id ,p.name as project_name,cl.status,cl.is_add,cl.commission,cl.money,cl.type')
                ->alias('cl')
                ->leftJoin('qt_project p', 'p.id = cl.product_id')
                ->where(['cl.store_id' => $userInfo['store_id']])
                ->whereBetweenTime($field, $getData['start_time'], $getData['end_time'])
                ->whereIn('cl.type', ['1', '6'])
                ->select();
            //dump($project);die;
            $result = [];
            foreach ($project as $key => $value) {
                //初始化数据
                $result[$value['product_id']]['project_name'] = $value['name'];
                $result[$value['product_id']]['spot'] = 0;//点钟
                $result[$value['product_id']]['add_work'] = 0;//加班点钟
                $result[$value['product_id']]['row'] = 0;//排钟
                $result[$value['product_id']]['add_chose'] = 0;//加班选钟
                $result[$value['product_id']]['call'] = 0;//call钟
                $result[$value['product_id']]['add'] = 0;//加钟
                $result[$value['product_id']]['number'] = $value['number'];//次数
                $result[$value['product_id']]['money'] = $value['money'];//金额
                foreach ($data as $k => $v) {
                    if ($v['product_id'] == $value['product_id']) {
                        //点钟数量
                        if ($v['status'] == '2' && $v['is_add'] == '1') {
                            $result[$value['product_id']]['spot'] += 1;
                        }
                        //call钟数量
                        if ($v['status'] == '3') {
                            $result[$value['product_id']]['call'] += 1;
                        }
                        //加班数量
                        if ($v['is_add'] == '2') {
                            $result[$value['product_id']]['add_work'] += 1;
                        }
                        //排钟数量
                        if ($v['status'] == '1'  || $v['status'] == '4' && $v['is_add'] == '1' ) {
                            $result[$value['product_id']]['row'] += 1;
                        }
                        //加钟数量
                        if ($v['status'] == '5' && $v['is_add'] == '1') {
                            $result[$value['product_id']]['add'] += 1;
                        }
                    }
                }
            }
            $result = array_values($result);
            $this->result('查询成功', $result, 200);
        }
    }


    /**
     * 收银结算月报表
     */
    public function settlement()
    {
        if ($this->request->isPost()) {
            $getData = $this->request->post();
            $userInfo = $this->userInfo;
            if (empty($getData['start_time']) || empty($getData['end_time'])) {
                $this->result('缺少时间类型', '', 204);
            }
            $startTime = strtotime($getData['start_time']);
            $endTime = strtotime($getData['end_time']);
            $groupData = Db::name('qt_check_out_pay')
                ->field('co.time')
                ->alias('co')
                ->leftJoin('qt_pay_type t', 't.id = co.pay_id ')
                ->where(['co.store_id' => $userInfo['store_id']])
                ->whereBetweenTime('co.create_time', $startTime, $endTime)
                ->group('time')
                ->select();
            //dump($groupData);
            $payType = Db::name('qt_pay_type')->where('store_id', '0')->column('*', 'id');
            $payTypeSearch = Db::name('qt_pay_type')->where('store_id', $userInfo['store_id'])->column('id');
            $basePayType = ['1', '2', '3', '4', '5', '6'];
            $payTypeSearch = array_merge($basePayType, $payTypeSearch);
            foreach ($groupData as $key => $value) {
                $groupData[$key]['total_income'] = 0;//总收入
                foreach ($payType as $k => $v) {
                    $groupData[$key][$k] = 0;
                }
                $groupData[$key]['card_money'] = 0;//会员卡支付
                $groupData[$key]['card_deduction'] = 0;//会员划卡
                $groupData[$key]['cash_achievement'] = 0;//现金业绩
                $groupData[$key]['total_consumption'] = 0;//消费合计
                $groupData[$key]['charge'] = 0;//挂账金额
                $groupData[$key]['charge_recovery'] = 0;//挂账回收
                foreach ($payType as $k => $v) {
                    $groupData[$key]['vip' . $k] = 0;
                }
                $groupData[$key]['total_recharge'] = 0;//充值合计
            }
            //dump($groupData);
            foreach ($groupData as $key => $value) {
                //总收入
                $groupData[$key]['total_income'] =
                    Db::name('qt_check_out_pay')
                        ->where(['store_id' => $userInfo['store_id']])
                        ->where('time', $value['time'])
                        ->whereIn('type', ['1','2','3'])
                        ->sum('pay_money');
                //现金业绩
                $groupData[$key]['cash_achievement'] =
                    Db::name('qt_check_out_pay')
                        ->where(['store_id' => $userInfo['store_id']])
                        ->where('time', $value['time'])
                        ->whereIn('pay_id', $payTypeSearch)
                        ->where('type', '2')
                        ->sum('pay_money');
                //消费合计
                $groupData[$key]['total_consumption'] =
                    Db::name('qt_check_out_pay')
                        ->where(['store_id' => $userInfo['store_id']])
                        ->where('time', $value['time'])
                        ->whereIn('pay_id', $payTypeSearch)
                        ->whereNotIn('pay_id', ['6'])
                        ->whereIn('type', ['2'])
                        ->sum('pay_money');

                $groupData[$key]['total_recharge'] = Db::name('qt_check_out_pay')
                    ->where(['store_id' => $userInfo['store_id']])
                    ->where('time', $value['time'])
                    ->whereIn('pay_id', $payTypeSearch)
                    ->whereIn('type', ['1','3'])
                    ->sum('pay_money');
                foreach ($payType as $k => $v) {
                    $groupData[$key][$k] = Db::name('qt_check_out_pay')
                        ->where(['store_id' => $userInfo['store_id']])
                        ->where('time', $value['time'])
                        ->where('pay_id', $k)
                        ->whereIn('type', ['2'])
                        ->sum('pay_money');
                    $groupData[$key]['vip' . $k] = Db::name('qt_check_out_pay')
                        ->where(['store_id' => $userInfo['store_id']])
                        ->where('time', $value['time'])
                        ->where('pay_id', $k)
                        ->whereIn('type', ['3'])
                        ->sum('pay_money');

                }
            }
            $result['title'] = array_values($payType);
            $result['result'] = $groupData;
            $result['total_money'] = Db::name('qt_check_out_pay')
                ->where(['store_id' => $userInfo['store_id']])
                ->whereIn('type', ['1','3'])
                ->sum('pay_money');
            $this->result('查询成功', $result, 200);
        }
    }


    /**
     * 营业报表
     */
    public function businessStatement()
    {
        if ($this->request->isPost()) {
            $getData = $this->request->post();
            $userInfo = $this->userInfo;
            //现金充值
            $revenue_cash = Db::name('qt_check_out_pay')
                ->whereBetweenTime('create_time', strtotime($getData['start_time']), strtotime($getData['end_time']))
                ->where(['store_id' => $this->userInfo['store_id'], 'type' => '1', 'pay_id' => '1'])
                ->sum('pay_money');
            //银联充值
            $revenue_union_pay = Db::name('qt_check_out_pay')
                ->whereBetweenTime('create_time', strtotime($getData['start_time']), strtotime($getData['end_time']))
                ->where(['store_id' => $this->userInfo['store_id'], 'type' => '1', 'pay_id' => '2'])
                ->sum('pay_money');
            //微信充值
            $revenue_wechat_pay = Db::name('qt_check_out_pay')
                ->whereBetweenTime('create_time', strtotime($getData['start_time']), strtotime($getData['end_time']))
                ->where(['store_id' => $this->userInfo['store_id'], 'type' => '1', 'pay_id' => '3'])
                ->sum('pay_money');
            //支付宝充值
            $revenue_ali_pay = Db::name('qt_check_out_pay')
                ->whereBetweenTime('create_time', strtotime($getData['start_time']), strtotime($getData['end_time']))
                ->where(['store_id' => $this->userInfo['store_id'], 'type' => '1', 'pay_id' => '4'])
                ->sum('pay_money');
            //赠送金额
            $revenue_give_pay = Db::name('qt_viper_card_recharge_log')
                ->whereBetweenTime('create_time', strtotime($getData['start_time']), strtotime($getData['end_time']))
                ->where(['store_id' => $this->userInfo['store_id']])
                ->sum('give_amount');
            $revenue_amount = Db::name('qt_viper_card_recharge_log')
                ->whereBetweenTime('create_time', strtotime($getData['start_time']), strtotime($getData['end_time']))
                ->where(['store_id' => $this->userInfo['store_id']])
                ->sum('amount');
            //非营收汇总
            $non_revenue = [
                'cash' => $revenue_cash,//现金
                'union_pay' => $revenue_union_pay,//银联
                'wechat_pay' => $revenue_wechat_pay,//微信
                'ali_pay' => $revenue_ali_pay,//支付宝
                'free' => 0,//免单
                'give' => $revenue_give_pay,//赠送
                'total' => $revenue_amount + $revenue_give_pay,//汇总
            ];
            //现金充值
            $cash = Db::name('qt_check_out_pay')
                ->whereBetweenTime('create_time', strtotime($getData['start_time']), strtotime($getData['end_time']))
                ->where(['store_id' => $this->userInfo['store_id'], 'type' => '1', 'pay_id' => '1'])
                ->sum('pay_money');
            //银联充值
            $union_pay = Db::name('qt_check_out_pay')
                ->whereBetweenTime('create_time', strtotime($getData['start_time']), strtotime($getData['end_time']))
                ->where(['store_id' => $this->userInfo['store_id'], 'type' => '1', 'pay_id' => '2'])
                ->sum('pay_money');
            //微信充值
            $wechat_pay = Db::name('qt_check_out_pay')
                ->whereBetweenTime('create_time', strtotime($getData['start_time']), strtotime($getData['end_time']))
                ->where(['store_id' => $this->userInfo['store_id'], 'type' => '1', 'pay_id' => '3'])
                ->sum('pay_money');
            //支付宝充值
            $ali_pay = Db::name('qt_check_out_pay')
                ->whereBetweenTime('create_time', strtotime($getData['start_time']), strtotime($getData['end_time']))
                ->where(['store_id' => $this->userInfo['store_id'], 'type' => '1', 'pay_id' => '4'])
                ->sum('pay_money');
            //赠送金额
            $give_pay = Db::name('qt_viper_card_recharge_log')
                ->whereBetweenTime('create_time', strtotime($getData['start_time']), strtotime($getData['end_time']))
                ->where(['store_id' => $this->userInfo['store_id']])
                ->sum('give_amount');
            $amount = Db::name('qt_viper_card_recharge_log')
                ->whereBetweenTime('create_time', strtotime($getData['start_time']), strtotime($getData['end_time']))
                ->where(['store_id' => $this->userInfo['store_id']])
                ->sum('amount');
            $non_revenue_detail['vip_recharge'] = [
                'cash' => $cash,//现金
                'union_pay' => $union_pay,//银联
                'wechat_pay' => $wechat_pay,//微信
                'ali_pay' => $ali_pay,//支付宝
                'free' => 0,//免单
                'give' => $give_pay,//赠送
                'total' => $give_pay + $amount,//汇总
            ];


            //开卡现金充值
            $open_cash_pay = Db::name('qt_viper_card_recharge_log')
                ->whereBetweenTime('create_time', strtotime($getData['start_time']), strtotime($getData['end_time']))
                ->where(['store_id' => $this->userInfo['store_id'], 'is_first' => '1'])
                ->whereLike('pay_content', '%现金%')
                ->sum('amount');
            //开卡银联充值
            $open_union_pay = Db::name('qt_viper_card_recharge_log')
                ->whereBetweenTime('create_time', strtotime($getData['start_time']), strtotime($getData['end_time']))
                ->where(['store_id' => $this->userInfo['store_id'], 'is_first' => '1'])
                ->whereLike('pay_content', '%银联%')
                ->sum('amount');
            //开卡微信充值
            $open_wechat_pay = Db::name('qt_viper_card_recharge_log')
                ->whereBetweenTime('create_time', strtotime($getData['start_time']), strtotime($getData['end_time']))
                ->where(['store_id' => $this->userInfo['store_id'], 'is_first' => '1'])
                ->whereLike('pay_content', '%微信%')
                ->sum('amount');
            //开卡微信充值
            $open_ali_pay = Db::name('qt_viper_card_recharge_log')
                ->whereBetweenTime('create_time', strtotime($getData['start_time']), strtotime($getData['end_time']))
                ->where(['store_id' => $this->userInfo['store_id'], 'is_first' => '1'])
                ->whereLike('pay_content', '%支付宝%')
                ->sum('amount');

            //开卡微信充值
            $open_give = Db::name('qt_viper_card_recharge_log')
                ->whereBetweenTime('create_time', strtotime($getData['start_time']), strtotime($getData['end_time']))
                ->where(['store_id' => $this->userInfo['store_id'], 'is_first' => '1'])
                ->sum('give_amount');
            //开卡充值金额
            $open_amount = Db::name('qt_viper_card_recharge_log')
                ->whereBetweenTime('create_time', strtotime($getData['start_time']), strtotime($getData['end_time']))
                ->where(['store_id' => $this->userInfo['store_id'], 'is_first' => '1'])
                ->sum('amount');
            //会员开卡
            $non_revenue_detail['vip_open'] = [
                'cash' => $open_cash_pay,//现金
                'union_pay' => $open_union_pay,//银联
                'wechat_pay' => $open_wechat_pay,//微信
                'ali_pay' => $open_ali_pay,//支付宝
                'free' => 0,//免单
                'give' => $open_give,//赠送
                'total' => $open_amount + $open_give,//汇总
            ];

            $vip_user_number = Db::name('qt_viper_card_recharge_log')
                ->whereBetweenTime('create_time', strtotime($getData['start_time']), strtotime($getData['end_time']))
                ->where(['store_id' => $this->userInfo['store_id'], 'is_first' => '1'])
                ->group('viper_card_id')
                ->count();
            $vip_user_recharge = Db::name('qt_viper_card_recharge_log')
                ->whereBetweenTime('create_time', strtotime($getData['start_time']), strtotime($getData['end_time']))
                ->where(['store_id' => $this->userInfo['store_id'], 'is_first' => '2'])
                ->group('viper_card_id')
                ->count();

            $charge_number = Db::name('qt_charge_item')
                ->whereBetweenTime('create_time', strtotime($getData['start_time']), strtotime($getData['end_time']))
                ->where(['store_id' => $this->userInfo['store_id']])
                ->group('charge_id')
                ->count();
            $charge_money = Db::name('qt_charge_item')
                ->alias('item')
                ->leftJoin('qt_check_out out', 'out.id = item.check_out_id')
                ->whereBetweenTime('item.create_time', strtotime($getData['start_time']), strtotime($getData['end_time']))
                ->where(['item.store_id' => $this->userInfo['store_id']])
                ->sum('out.total_money');
            $non_revenue_detail['vip_user_number'] = $vip_user_number;//新增会员
            $non_revenue_detail['vip_user_recharge'] = $vip_user_recharge;//续充会员
            $non_revenue_detail['charge_number'] = $charge_number;//挂账人数
            $non_revenue_detail['charge_money'] = $charge_money;//挂账金额
            //营业总额
            $turnover = Db::name('qt_check_out_pay')
                ->whereBetweenTime('create_time', strtotime($getData['start_time']), strtotime($getData['end_time']))
                ->where(['store_id' => $this->userInfo['store_id']])
                ->where('type','2')
                ->sum('pay_money');
            $cash_consumption = Db::name('qt_check_out_pay')
                ->whereBetweenTime('create_time', strtotime($getData['start_time']), strtotime($getData['end_time']))
                ->where(['pay_id' => '1', 'store_id' => $this->userInfo['store_id'], 'type' => '2'])
                ->sum('pay_money');
            $cash_achievement = Db::name('qt_check_out_pay')
                ->whereBetweenTime('create_time', strtotime($getData['start_time']), strtotime($getData['end_time']))
                ->where(['store_id' => $this->userInfo['store_id'],'type'=>'2'])
                ->where('pay_id', '<', '6')
                ->sum('pay_money');
            $other_income = Db::name('qt_check_out_pay')
                ->whereBetweenTime('create_time', strtotime($getData['start_time']), strtotime($getData['end_time']))
                ->where(['store_id' => $this->userInfo['store_id']])
                ->where('pay_id', '>', '6')
                ->sum('pay_money');
            $consumption_number = Db::name('qt_check_out')
                ->whereBetweenTime('cashier_time', strtotime($getData['start_time']), strtotime($getData['end_time']))
                ->where(['store_id' => $this->userInfo['store_id']])
                ->count();
            $totalMoney = Db::name('qt_check_out')
                ->whereBetweenTime('cashier_time', strtotime($getData['start_time']), strtotime($getData['end_time']))
                ->where(['store_id' => $this->userInfo['store_id']])
                ->sum('payment_money');
            if ($consumption_number == 0) {
                $average_consumption = 0;
            } else {
                $average_consumption = bcdiv($totalMoney, $consumption_number, '2');
            }
            //会员消费数
            $vip_number = Db::name('qt_check_out_pay')
                ->whereBetweenTime('create_time', strtotime($getData['start_time']), strtotime($getData['end_time']))
                ->where(['store_id' => $this->userInfo['store_id']])
                ->where('pay_id', '=', '6')
                ->count();
            //会员消费总额
            $vip_money = Db::name('qt_check_out_pay')
                ->whereBetweenTime('create_time', strtotime($getData['start_time']), strtotime($getData['end_time']))
                ->where(['store_id' => $this->userInfo['store_id']])
                ->where('pay_id', '=', '6')
                ->sum('card_amount');
            //会员消费平均数
            if ($vip_number == 0) {
                $vip_average_consumption = 0;
            } else {
                $vip_average_consumption = bcdiv($vip_money, $vip_number, '2');
            }

            $checkOutId = Db::name('qt_check_out')
                ->whereBetweenTime('cashier_time', strtotime($getData['start_time']), strtotime($getData['end_time']))
                ->where(['store_id' => $this->userInfo['store_id']])
                ->column('id');
            //主项数量
            $main_project =  Db::name('qt_check_out_item')
                ->alias('item')
                ->leftJoin('qt_project p','p.id = item.link_id')
                ->whereIn('item.check_out_id',$checkOutId)
                ->where(['item.link_type'=>'1','p.type'=>'1'])
                ->count();
            //副项数量
            $other_project =  Db::name('qt_check_out_item')
                ->alias('item')
                ->leftJoin('qt_project p','p.id = item.link_id')
                ->whereIn('item.check_out_id',$checkOutId)
                ->where(['item.link_type'=>'1','p.type'=>'1'])
                ->count();

            //商品数量
            $goods_number =  Db::name('qt_check_out_item')
                ->alias('item')
                ->leftJoin('qt_project p','p.id = item.link_id')
                ->whereIn('item.check_out_id',$checkOutId)
                ->where(['item.link_type'=>'2'])
                ->sum('total_num');
            //商品金额
            $goods_money =  Db::name('qt_check_out_item')
                ->alias('item')
                ->leftJoin('qt_product_order_item gitem','gitem.id = item.link_order_id')
                ->whereIn('item.check_out_id',$checkOutId)
                ->where(['item.link_type'=>'2'])
                ->sum('actual_receipt');
            //营业总额
            $total = [
                'turnover' => $turnover,//营业额
                'cash_consumption' => $cash_consumption,//现金消费
                'cash_achievement' => $cash_achievement,//现金业绩
                'other_income' => $other_income,//其他收入
                'consumption_number' => $consumption_number,//消费人数
                'average_consumption' => $average_consumption,//人均消费
                'vip_number' => $vip_number,//会员消费人数
                'vip_average_consumption' => $vip_average_consumption,//会员人均消费
                'main_project' => $main_project,//主项
                'other_project' => $other_project,//副项
                'goods_number' => $goods_number,//商品数量
                'goods_money' => $goods_money,//商品金额
            ];
            $sql = Db::name('qt_check_out_pay')
                ->field('co.user_id,count(co.id) as number,u.real_name,sum(co.pay_money) as pay_money,sum(co.card_money) as card_money')
                ->alias('co')
                ->leftJoin('qt_pay_type t', 't.id = co.pay_id ')
                ->leftJoin('qt_users u', 'co.user_id = u.id ')
                //->leftJoin('qt_check_out ck', 'co.check_out_id = ck.id ')
                ->where(['co.store_id' => $userInfo['store_id']])
                //->where(['ck.is_revoke' => 2])//非撤销
                ->whereBetweenTime('co.create_time', $getData['start_time'], $getData['end_time']);
            if (!empty($getData['user_id'])) {
                $sql->where('co.user_id', $getData['user_id']);
            }
            //顶部收银信息
            $groupData = $sql->group('user_id')->select();
            $payType = Db::name('qt_pay_type')->where('store_id', $userInfo['store_id'])->column('*', 'id');
            foreach ($groupData as $key => $value) {
                foreach ($payType as $k => $v) {
                    $groupData[$key][$k] = 0;
                }
                $groupData[$key]['card_money'] = Db::name('qt_check_out_pay')
                    ->whereBetweenTime('create_time', strtotime($getData['start_time']), strtotime($getData['end_time']))
                    ->where(['user_id' => $value['user_id']])
                    ->where('pay_id', '=', '6')
                    ->sum('pay_money');//会员卡支付
                $groupData[$key]['card_deduction'] = 0;//会员划卡
                $groupData[$key]['cash_achievement'] = Db::name('qt_check_out_pay')
                    ->whereBetweenTime('create_time', strtotime($getData['start_time']), strtotime($getData['end_time']))
                    ->where(['user_id' => $value['user_id']])
                    ->whereIn('pay_id', ['1','2','3','4','5','7'])
                    ->sum('pay_money');//现金业绩
                $groupData[$key]['total_consumption'] = 0;//消费合计
                $groupData[$key]['charge'] = Db::name('qt_check_out')
                    ->whereBetweenTime('cashier_time', strtotime($getData['start_time']), strtotime($getData['end_time']))
                    ->where(['cashier_id' => $value['user_id'],'order_type'=>'2','store_id'=>$userInfo['store_id']])
                    ->sum('total_money');//挂账金额
                $groupData[$key]['charge_recovery'] = Db::name('qt_check_out_pay')
                    ->whereBetweenTime('create_time', strtotime($getData['start_time']), strtotime($getData['end_time']))
                    ->where(['user_id' => $value['user_id'],'store_id'=>$userInfo['store_id']])
                    ->sum('charge_recovery');//挂账回收
            }
            $result['check_out'] = $groupData;//收银数据
            $result['non_revenue'] = $non_revenue;//非营收汇总
            $result['non_revenue_detail'] = $non_revenue_detail;//非营业收入统计
            $result['total'] = $total;//营业总额
            $result['title'] = $payType;//收入标题
            $this->result('查询成功', $result, 200);
        }
    }


    /**
     * 楼面业绩报表单个详情
     */
    public function floorReceiptsSingle()
    {
        if ($this->request->isPost()) {
            $getData = $this->request->post();
            $userInfo = $this->userInfo;
            $field = 'created_time';
            $startTime = strtotime($getData['start_time']);
            $endTime = strtotime($getData['end_time']);
            $projectData = Db::name('qt_commission_log')
                ->field('log.product_id as project_id,p.name as project_name,count(product_id) as total_number,sum(money) as total_money,sum(receiver) as total_receiver')
                ->alias('log')
                ->leftJoin('qt_project p', 'p.id = log.product_id')
                ->where(['log.store_id' => $userInfo['store_id'], 'log.cashier_id' => $getData['user_id']])
                ->whereBetweenTime($field, $startTime, $endTime)
                ->where('log.type', '1')
                ->group('log.product_id')
                ->select();
            foreach ($projectData as $key => $value) {
                $projectData[$key]['row'] = Db::name('qt_commission_log')
                    ->field('count(id) as total_number,type')
                    ->where(['store_id' => $userInfo['store_id'], 'cashier_id' => $getData['user_id'], 'is_add' => '1', 'product_id' => $value['project_id']])
                    ->whereBetweenTime($field, $startTime, $endTime)
                    ->where(['type' => '1', 'status' => '1'])
                    ->count();
                $projectData[$key]['clock'] = Db::name('qt_commission_log')
                    ->field('count(id) as total_number,type')
                    ->where(['store_id' => $userInfo['store_id'], 'cashier_id' => $getData['user_id'], 'is_add' => '1', 'product_id' => $value['project_id']])
                    ->whereBetweenTime($field, $startTime, $endTime)
                    ->where(['type' => '1', 'status' => '2'])
                    ->count();
                $projectData[$key]['call'] = Db::name('qt_commission_log')
                    ->field('count(id) as total_number,type')
                    ->where(['store_id' => $userInfo['store_id'], 'cashier_id' => $getData['user_id'], 'is_add' => '1', 'product_id' => $value['project_id']])
                    ->whereBetweenTime($field, $startTime, $endTime)
                    ->where(['type' => '1', 'status' => '3'])
                    ->count();
                $projectData[$key]['chose'] = Db::name('qt_commission_log')
                    ->field('count(id) as total_number,type')
                    ->where(['store_id' => $userInfo['store_id'], 'cashier_id' => $getData['user_id'], 'is_add' => '1', 'product_id' => $value['project_id']])
                    ->whereBetweenTime($field, $startTime, $endTime)
                    ->where(['type' => '1', 'status' => '4'])
                    ->count();
                $projectData[$key]['add'] = Db::name('qt_commission_log')
                    ->field('count(id) as total_number,type')
                    ->where(['store_id' => $userInfo['store_id'], 'cashier_id' => $getData['user_id'], 'is_add' => '1', 'product_id' => $value['project_id']])
                    ->whereBetweenTime($field, $startTime, $endTime)
                    ->where(['type' => '1', 'status' => '5'])
                    ->count();
            }
            $cardData = Db::name('qt_commission_log')
                ->field('type.name as viper_type_name,count(log.id) as total_number,sum(money) as total_money,sum(receiver) as total_receiver')
                ->alias('log')
                ->leftJoin('qt_viper_type type', 'type.id = log.card_type_id')
                ->where(['log.store_id' => $userInfo['store_id'], 'log.cashier_id' => $getData['user_id']])
                ->whereBetweenTime($field, $startTime, $endTime)
                ->whereIn('log.type', ['3', '4', '5', '7'])
                ->group('log.card_type_id')
                ->select();

            $goodsData = Db::name('qt_commission_log')
                ->field('g.name as goods_name,sum(item.goods_num) as total_number,sum(money) as total_money,sum(log.receiver) as total_receiver')
                ->alias('log')
                ->leftJoin('qt_goods g', 'g.id = log.product_id')
                ->leftJoin('qt_product_order_item item', 'log.order_project_id = item.id')
                ->where(['log.store_id' => $userInfo['store_id'], 'log.cashier_id' => $getData['user_id']])
                ->whereBetweenTime($field, $startTime, $endTime)
                ->where('log.type', '2')
                ->group('log.product_id')
                ->select();
            $result['project_data'] = $projectData;
            $result['card_data'] = $cardData;
            $result['goods_data'] = $goodsData;
            $this->result('查询成功', $result, 200);
        }
        $this->result('无效的请求', '', 204);
    }


    /**
     * 技师业绩报表单个详情
     */
    public function netReceiptsSingle()
    {
        if ($this->request->isPost()) {
            $getData = $this->request->post();
            $userInfo = $this->userInfo;
            $field = 'created_time';
            $leftField = 'log.created_time';
            $startTime = strtotime($getData['start_time']);
            $endTime = strtotime($getData['end_time']);
            $projectData = Db::name('qt_commission_log')
                ->field('log.product_id as project_id,p.name as project_name,count(product_id) as total_number,sum(money) as total_money,sum(commission) as total_receiver')
                ->alias('log')
                ->leftJoin('qt_project p', 'p.id = log.product_id')
                ->where(['log.store_id' => $userInfo['store_id'], 'log.user_id' => $getData['user_id']])
                ->whereBetweenTime($field, $startTime, $endTime)
                ->where('log.type', '1')
                ->group('log.product_id')
                ->select();
            foreach ($projectData as $key => $value) {
                $projectData[$key]['row'] = Db::name('qt_commission_log')
                    ->field('count(id) as total_number,type')
                    ->where(['store_id' => $userInfo['store_id'], 'user_id' => $getData['user_id'],  'product_id' => $value['project_id']])
                    ->whereBetweenTime($field, $startTime, $endTime)
                    ->where(['type' => '1', 'status' => '1'])
                    ->count();
                $projectData[$key]['clock'] = Db::name('qt_commission_log')
                    ->field('count(id) as total_number,type')
                    ->where(['store_id' => $userInfo['store_id'], 'user_id' => $getData['user_id'],  'product_id' => $value['project_id']])
                    ->whereBetweenTime($field, $startTime, $endTime)
                    ->where(['type' => '1', 'status' => '2'])
                    ->count();
                $projectData[$key]['call'] = Db::name('qt_commission_log')
                    ->field('count(id) as total_number,type')
                    ->where(['store_id' => $userInfo['store_id'], 'user_id' => $getData['user_id'], 'product_id' => $value['project_id']])
                    ->whereBetweenTime($field, $startTime, $endTime)
                    ->where(['type' => '1', 'status' => '3'])
                    ->count();
                $projectData[$key]['chose'] = Db::name('qt_commission_log')
                    ->field('count(id) as total_number,type')
                    ->where(['store_id' => $userInfo['store_id'], 'user_id' => $getData['user_id'],  'product_id' => $value['project_id']])
                    ->whereBetweenTime($field, $startTime, $endTime)
                    ->where(['type' => '1', 'status' => '4'])
                    ->count();
                $projectData[$key]['add'] = Db::name('qt_commission_log')
                    ->field('count(id) as total_number,type')
                    ->where(['store_id' => $userInfo['store_id'], 'user_id' => $getData['user_id'],  'product_id' => $value['project_id']])
                    ->whereBetweenTime($field, $startTime, $endTime)
                    ->where(['type' => '1', 'status' => '5'])
                    ->count();
            }

            $projectDetailData = Db::name('qt_commission_log')
                ->field('log.product_id as project_id,p.name as project_name,op.order_type,r.name as room_name,u.work_number,log.commission')
                ->alias('log')
                ->leftJoin('qt_project p', 'p.id = log.product_id')
                ->leftJoin('qt_order_project op', 'op.id = log.order_project_id')
                ->leftJoin('qt_room r', 'r.id = op.room_id')
                ->leftJoin('qt_users u', 'u.id = op.engineer_id')
                ->where(['log.store_id' => $userInfo['store_id'], 'log.user_id' => $getData['user_id']])
                ->whereBetweenTime($leftField, $startTime, $endTime)
                ->where('log.type', '1')
                ->select();
            $cardData = Db::name('qt_commission_log')
                ->field('type.name as viper_type_name,count(log.id) as total_number,sum(money) as total_money,sum(commission) as total_receiver')
                ->alias('log')
                ->leftJoin('qt_viper_type type', 'type.id = log.card_type_id')
                ->where(['log.store_id' => $userInfo['store_id'], 'log.user_id' => $getData['user_id']])
                ->whereBetweenTime($field, $startTime, $endTime)
                ->whereIn('log.type', ['3', '4', '5', '7'])
                ->group('log.card_type_id')
                ->select();

            $goodsData = Db::name('qt_commission_log')
                ->field('g.name as goods_name,count(log.id) as total_number,sum(money) as total_money,sum(commission) as total_receiver')
                ->alias('log')
                ->leftJoin('qt_goods g', 'g.id = log.product_id')
                ->where(['log.store_id' => $userInfo['store_id'], 'log.user_id' => $getData['user_id']])
                ->whereBetweenTime($field, $startTime, $endTime)
                ->where('log.type', '2')
                ->group('log.product_id')
                ->select();
            $result['project_data'] = $projectData;
            $result['project_detail_data'] = $projectDetailData;
            $result['card_data'] = $cardData;
            $result['goods_data'] = $goodsData;
            $this->result('查询成功', $result, 200);
        }
        $this->result('无效的请求', '', 204);
    }


    /**
     * 楼牌统计技师
     */
    public function storeUser()
    {
        if ($this->request->isPost()) {
            $getData = $this->request->post();
            if (!isset($getData['type']) || empty($getData['type'])) {
                $this->result('缺少时间类型', '', 204);
            }
            $userInfo = $this->userInfo;
            if ($getData['type'] == '1') {
                $field = 'created_time';
            } else {
                $field = 'updated_time';
            }
            //下单时间
            if ($getData['time'] == '1') {
                //今天
                $time = 'today';
            }
            if ($getData['time'] == '2') {
                //昨天
                $time = 'yesterday';
            }
            if ($getData['time'] == '3') {
                //本月
                $time = 'month';
            }
            if ($getData['time'] == '4') {
                //上月
                $time = 'last month';
            }
            //用户数据
            $userRes = Db::name('qt_commission_log')
                ->field('cl.id,cl.user_id,u.work_number')
                ->alias('cl')
                ->leftJoin('qt_users u', 'u.id = cl.user_id')
                ->where(['cl.store_id' => $userInfo['store_id']])
                ->whereTime('cl.' . $field, $time)
                ->group('cl.user_id')
                ->select();
            //总数据
            $dataRes = Db::name('qt_commission_log')
                ->where(['store_id' => $userInfo['store_id']])
                ->whereTime($field, $time)
                ->select();
            //构造返回数据
            static $result = [];
            foreach ($userRes as $key => $value) {
                $result['data'][$value['user_id']]['user_id'] = $value['user_id'];
                $result['data'][$value['user_id']]['project'] = 0;
                $result['data'][$value['user_id']]['product'] = 0;
                $result['data'][$value['user_id']]['money'] = 0;
                $result['data'][$value['user_id']]['card'] = 0;
                $result['data'][$value['user_id']]['commission'] = 0;
                $result['data'][$value['user_id']]['work_number'] = $value['work_number'];
                $result['total']['project'] = 0;
                $result['total']['product'] = 0;
                $result['total']['money'] = 0;
                $result['total']['card'] = 0;
                $result['total']['commission'] = 0;

            }
            //数据填充
            //订单类型 1 轮排 2  点钟 3 call钟  4 优轮 5 加班
            foreach ($dataRes as $key => $value) {
                if ($value['status'] == '1') {
                    $result['data'][$value['user_id']]['project'] += 1;
                    $result['total']['project'] += 1;
                }
                if ($value['type'] == '2') {
                    $result['data'][$value['user_id']] ['product'] += 1;
                    $result['total']['product'] += 1;
                }
                if ($value['type'] == '3') {
                    $result['data'][$value['user_id']]['card'] += 1;
                    $result['total']['card'] += 1;
                }
                $result['data'][$value['user_id']]['money'] += $value['money'];
                $result['total']['money'] += $value['money'];
                $result['data'][$value['user_id']]['commission'] += $value['commission'];
                $result['total']['commission'] += $value['commission'];
            }
            if (empty($result)) {
                $result = [];
            } else {
                $result['data'] = array_values($result['data']);
            }
            $this->result('查询成功', $result, 200);
        }
    }


    /**
     * 楼牌统计技师工作
     */
    public function storeUserWork()
    {
        if ($this->request->isPost()) {
            $getData = $this->request->post();
            if (!isset($getData['type']) || empty($getData['type'])) {
                $this->result('缺少时间类型', '', 204);
            }
            $userInfo = $this->userInfo;
            if ($getData['type'] == '1') {
                $field = 'created_time';
            } else {
                $field = 'updated_time';
            }
            if (isset($getData['start_time'])) {
                $startTime = strtotime($getData['start_time']);
            }
            if (isset($getData['end_time'])) {
                $endTime = strtotime($getData['end_time']);
            }
            //用户数据
            $userRes = Db::name('qt_commission_log')
                ->field('cl.id,cl.user_id,u.real_name')
                ->alias('cl')
                ->leftJoin('qt_users u', 'u.id = cl.cashier_id')
                ->where(['cl.store_id' => $userInfo['store_id']])
                ->whereBetweenTime('created_time', $startTime, $endTime)
                ->group('cl.cashier_id')
                ->select();
            //总数据
            $dataRes = Db::name('qt_commission_log')
                ->where(['store_id' => $userInfo['store_id']])
                ->whereBetweenTime('created_time', $startTime, $endTime)
                ->select();
            //构造返回数据
            static $result = [];
            foreach ($userRes as $key => $value) {
                $result['data'][$value['user_id']]['user_id'] = $value['user_id'];
                $result['data'][$value['user_id']]['row'] = 0;
                $result['data'][$value['user_id']]['clock'] = 0;
                $result['data'][$value['user_id']]['call'] = 0;
                $result['data'][$value['user_id']]['card'] = 0;
                $result['data'][$value['user_id']]['add'] = 0;
                $result['data'][$value['user_id']]['add_work'] = 0;
                $result['data'][$value['user_id']]['product'] = 0;
                $result['data'][$value['user_id']]['real_name'] = $value['real_name'];
                $result['total']['row'] = 0;
                $result['total']['clock'] = 0;
                $result['total']['call'] = 0;
                $result['total']['add'] = 0;
                $result['total']['add_work'] = 0;
                $result['total']['product'] = 0;
            }
            //数据填充
            //订单类型 1 轮排 2  点钟 3 call钟  4 优轮 5 加钟 6 加班
            foreach ($dataRes as $key => $value) {
                if ($value['status'] == '1' || $value['status'] == '4') {
                    $result['data'][$value['user_id']]['row'] += 1;
                    $result['total']['row'] += 1;
                }
                if ($value['status'] == '2') {
                    $result['data'][$value['user_id']] ['clock'] += 1;
                    $result['total']['clock'] += 1;
                }
                if ($value['status'] == '3') {
                    $result['data'][$value['user_id']]['call'] += 1;
                    $result['total']['call'] += 1;
                }
                if ($value['status'] == '5') {
                    $result['data'][$value['user_id']]['add'] += 1;
                    $result['total']['add'] += 1;
                }
                if ($value['status'] == '6') {
                    $result['data'][$value['user_id']]['add_word'] += 1;
                    $result['total']['add_word'] += 1;
                }
                if ($value['type'] == '2') {
                    $result['data'][$value['user_id']]['product'] += $value['money'];
                    $result['total']['product'] += $value['money'];
                }
            }
            $result['data'] = array_values($result['data']);
            $this->result('查询成功', $result, 200);
        }
    }


    /**
     * 楼牌统计技师
     */
    public function reception()
    {
        if ($this->request->isPost()) {
            $getData = $this->request->post();
            if (!isset($getData['type']) || empty($getData['type'])) {
                $this->result('缺少时间类型', '', 204);
            }
            $userInfo = $this->userInfo;
            if ($getData['type'] == '1') {
                $field = 'created_time';
            } else {
                $field = 'updated_time';
            }
            //下单时间
            if ($getData['time'] == '1') {
                //今天
                $time = 'today';
            }
            if ($getData['time'] == '2') {
                //昨天
                $time = 'yesterday';
            }
            if ($getData['time'] == '3') {
                //本月
                $time = 'month';
            }
            if ($getData['time'] == '4') {
                //上月
                $time = 'last month';
            }
            //用户数据
            $userRes = Db::name('qt_commission_log')
                ->field('cl.id,cl.cashier_id,u.real_name')
                ->alias('cl')
                ->leftJoin('qt_users u', 'u.id = cl.cashier_id')
                ->where(['cl.store_id' => $userInfo['store_id']])
                ->whereTime('cl.' . $field, $time)
                ->group('cl.cashier_id')
                ->select();
            //总数据
            $dataRes = Db::name('qt_commission_log')
                ->where(['store_id' => $userInfo['store_id']])
                ->whereTime($field, $time)
                ->select();
            //构造返回数据
            static $result = [];
            foreach ($userRes as $key => $value) {
                $result['data'][$value['cashier_id']]['user_id'] = $value['cashier_id'];
                $result['data'][$value['cashier_id']]['project'] = 0;
                $result['data'][$value['cashier_id']]['product'] = 0;
                $result['data'][$value['cashier_id']]['money'] = 0;
                $result['data'][$value['cashier_id']]['card'] = 0;
                $result['data'][$value['cashier_id']]['commission'] = 0;
                $result['data'][$value['cashier_id']]['real_name'] = $value['real_name'];
                $result['total']['project'] = 0;
                $result['total']['product'] = 0;
                $result['total']['money'] = 0;
                $result['total']['card'] = 0;
                $result['total']['commission'] = 0;
            }
            //数据填充
            //订单类型 1 轮排 2  点钟 3 call钟  4 优轮 5 加班
            foreach ($dataRes as $key => $value) {
                if ($value['status'] == '1') {
                    $result['data'][$value['cashier_id']]['project'] += 1;
                    $result['total']['project'] += 1;
                }
                if ($value['type'] == '2') {
                    $result['data'][$value['cashier_id']] ['product'] += 1;
                    $result['total']['product'] += 1;
                }
                if ($value['type'] == '3') {
                    $result['data'][$value['cashier_id']]['card'] += 1;
                    $result['total']['card'] += 1;
                }
                $result['data'][$value['cashier_id']]['money'] += $value['money'];
                $result['total']['money'] += $value['money'];
            }
            if (empty($result)) {
                $this->result('查询成功', $result, 200);
            }
            $result['data'] = array_values($result['data']);
            $this->result('查询成功', $result, 200);
        }
    }


    /**
     * 门店业绩
     */
    public function store()
    {
        if ($this->request->isPost()) {
            $getData = $this->request->post();
            if (!isset($getData['time']) || empty($getData['time'])) {
                $this->result('缺少时间类型', '', 204);
            }
            $userInfo = $this->userInfo;
            //下单时间
            if ($getData['time'] == '1') {
                //今天
                $time = 'today';
            }
            if ($getData['time'] == '2') {
                //昨天
                $time = 'yesterday';
            }
            if ($getData['time'] == '3') {
                //本月
                $time = 'month';
            }
            if ($getData['time'] == '4') {
                //上月
                $time = 'last month';
            }
            //总金额
            $result['totalPrice'] = Db::name('qt_commission_log')
                ->where('store_id', $userInfo['store_id'])
                ->whereTime('updated_time', $time)->sum('money');
            //现金
            $result['totalCash'] = Db::name('qt_commission_log')
                ->where('pay_type', '4')
                ->where('store_id', $userInfo['store_id'])
                ->whereTime('updated_time', $time)
                ->sum('money');
            //其他收入
            $result['totalOther'] = Db::name('qt_commission_log')
                ->where('pay_type', '5')
                ->where('store_id', $userInfo['store_id'])
                ->whereTime('updated_time', $time)
                ->sum('money');
            //现金消费
            $result['totalCashConsumption'] = Db::name('qt_commission_log')
                ->where('pay_type', '5')
                ->where('store_id', $userInfo['store_id'])
                ->whereTime('updated_time', $time)
                ->sum('payment_price');
            //售卡业绩
            $result['totalCard'] = Db::name('qt_commission_log')
                ->where('type', '3')
                ->where('store_id', $userInfo['store_id'])
                ->whereTime('updated_time', $time)
                ->sum('money');
            //新增会员
            $result['totalAddUser'] = Db::name('qt_user_viper')
                ->where('store_id', $userInfo['store_id'])
                ->whereTime('create_time', $time)
                ->count();
            //商品业绩
            $result['totalProduct'] = Db::name('qt_commission_log')
                ->where('type', '2')
                ->where('store_id', $userInfo['store_id'])
                ->whereTime('updated_time', $time)
                ->sum('money');
            //消费人数
            $result['totalUser'] = Db::name('qt_commission_log')
                ->where('store_id', $userInfo['store_id'])
                ->whereTime('updated_time', $time)
                ->group('member')
                ->count();
            if ($result['totalUser'] == '0' || $result['totalPrice'] == '0') {
                $result['totalUserAverage'] = "0";
            } else {
                //人均
                $result['totalUserAverage'] = number_format(($result['totalPrice'] / $result['totalUser']), 2);
            }
            //会员消费数
            $result['totalUserConsumption'] = Db::name('qt_commission_log')
                ->field('cl.*')
                ->alias('cl')
                ->leftJoin('qt_user_viper uv', 'uv.id = cl.user_id')
                ->where('cl.store_id', $userInfo['store_id'])
                ->where('uv.store_id', '1')
                ->whereTime('cl.updated_time', $time)
                ->sum('payment_price');

            //会员续费
            $result['totalUserRenew'] = Db::name('qt_commission_log')
                ->alias('cl')
                ->leftJoin('qt_user_viper uv', 'uv.id = cl.user_id')
                ->where('cl.store_id', $userInfo['store_id'])
                ->where('cl.type', '4')
                ->whereTime('cl.updated_time', $time)
                ->sum('payment_price');
            //赠送金额
            $result['totalGiveMoney'] = Db::name('qt_commission_log')
                ->alias('cl')
                ->leftJoin('qt_user_viper uv', 'uv.id = cl.user_id')
                ->where('cl.store_id', $userInfo['store_id'])
                ->where('cl.type', 'in', ['3', '4'])
                ->whereTime('cl.updated_time', $time)
                ->sum('give_money');

            //项目提成
            $result['projectCommission'] = Db::name('qt_commission_log')
                ->where('store_id', $userInfo['store_id'])
                ->where('type', '1')
                ->whereTime('updated_time', $time)
                ->sum('commission');

            //项目业绩
            $result['projectAchievement'] = Db::name('qt_commission_log')
                ->where('store_id', $userInfo['store_id'])
                ->where('type', '1')
                ->whereTime('updated_time', $time)
                ->sum('payment_price');

            //接待客服
            $result['roomCount'] = Db::name('qt_commission_log')
                ->where('store_id', $userInfo['store_id'])
                ->where('type', 'in', ['3', '4'])
                ->whereTime('updated_time', $time)
                ->group('room_id')
                ->count();

            //售卡提成
            $result['cardCommission'] = Db::name('qt_commission_log')
                ->where('store_id', $userInfo['store_id'])
                ->where('type', '3')
                ->whereTime('updated_time', $time)
                ->sum('commission');

            $this->result('查询成功', $result, 200);
        }
    }

    /**
     * 统计图表
     */
    public function storeChart()
    {
        if ($this->request->isPost()) {
            $getData = $this->request->post();
            if (!isset($getData['time']) || empty($getData['time'])) {
                $this->result('缺少时间类型', '', 204);
            }
            $userInfo = $this->userInfo;
            //下单时间
            if ($getData['time'] == '3') {
                //本月
                $time = 'month';
            }
            if ($getData['time'] == '4') {
                //上月
                $time = 'last month';
            }
            //总金额
            $result['total'] = $this->getMoneyTotal($userInfo['store_id'], $time, '1');
            /****************** 其他收入 ***********************/
            $result['other'] = $this->getMoneyTotal($userInfo['store_id'], $time, '2');
            /****************** 柱状图 ***********************/
            $result['histogram'] = $this->histogram($result['total']['series']['data']);
            /****************** 会员充值 ***********************/
            $result['userRecharge'] = $this->getCardTotal($userInfo['store_id'], $time, '1');
            /****************** 会员消费 ***********************/
            $result['userConsumption'] = $this->getCardTotal($userInfo['store_id'], $time, '2');
            $this->result('查询成功', $result, 200);
        }
    }


    /**
     * 获取消费数据统计
     * @param $storeId [门店ID]
     * @param $time [时间类型] [1 今天 2 昨天 3 本月 4 上月]
     * @param $type [类型] [1营收 2 非营收]
     */
    public function getMoneyTotal($storeId, $time, $type)
    {
        if ($type == '2') {
            $where['type'] = '5';
        } else {
            $where = 1;
        }
        //总金额
        $result['totalPrice'] = Db::name('qt_commission_log')
            ->where('store_id', $storeId)
            ->where($where)
            ->whereTime('updated_time', $time)
            ->sum('money');
        //现金
        $result['totalCash'] = Db::name('qt_commission_log')
            ->where('pay_type', '4')
            ->where($where)
            ->where('store_id', $storeId)
            ->whereTime('updated_time', $time)
            ->sum('money');
        //微信
        $result['totalWechat'] = Db::name('qt_commission_log')
            ->where('pay_type', '1')
            ->where($where)
            ->where('store_id', $storeId)
            ->whereTime('updated_time', $time)
            ->sum('money');
        //支付宝
        $result['totalAlipay'] = Db::name('qt_commission_log')
            ->where('pay_type', '2')
            ->where($where)
            ->where('store_id', $storeId)
            ->whereTime('updated_time', $time)
            ->sum('money');
        //会员卡
        $result['totalCard'] = Db::name('qt_commission_log')
            ->where('pay_type', '3')
            ->where($where)
            ->where('store_id', $storeId)
            ->whereTime('updated_time', $time)
            ->sum('money');
        //优惠金额
        $result['totalCoupon'] = Db::name('qt_commission_log')
            ->where('store_id', $storeId)
            ->where($where)
            ->whereTime('updated_time', $time)
            ->sum('coupon_money');
        $data[0]['name'] = '支付宝';
        $data[0]['value'] = $result['totalAlipay'];
        $data[1]['name'] = '微信';
        $data[1]['value'] = $result['totalWechat'];
        $data[2]['name'] = '现金';
        $data[2]['value'] = $result['totalCash'];
        $data[3]['name'] = '会员卡';
        $data[3]['value'] = $result['totalCard'];
        $data[4]['name'] = '优惠券';
        $data[4]['value'] = $result['totalCoupon'];
        $res['total'] = $result['totalPrice'];
        $res['series']['data'] = $data;
        return $res;
    }

    /**
     * 获取门店项目详情
     */
    public function storeProject()
    {
        if ($this->request->isPost()) {
            $getData = $this->request->post();
            if (!isset($getData['time']) || empty($getData['time'])) {
                $this->result('缺少时间类型', '', 204);
            }
            $userInfo = $this->userInfo;
            //下单时间
            if ($getData['time'] == '3') {
                //本月
                $time = 'month';
            }
            if ($getData['time'] == '4') {
                //上月
                $time = 'last month';
            }
            if ($getData['type'] == '1') {
                $type = 'cl.created_time';
            }
            if ($getData['type'] == '2') {
                $type = 'cl.updated_time';
            }
            $groupRes = Db::name('qt_commission_log')
                ->field('cl.*，p.name')
                ->alias('cl')
                ->leftJoin('qt_project p', 'cl.product_id = p.id')
                ->where(['cl.store_id' => $userInfo['store_id'], 'cl.type' => '1'])
                ->whereTime($type, $time)
                ->group('cl.product_id')
                ->select();
            $result = [];

            foreach ($groupRes as $key => $value) {
                $result[$key]['name'] = $value['name'];
                $dataRes = Db::name('qt_commission_log')
                    ->alias('cl')
                    ->leftJoin('qt_project p', 'cl.product_id = p.id')
                    ->where(['cl.store_id' => $userInfo['store_id'], 'cl.type' => '1', 'cl.product_id' => $value['product_id']])
                    ->whereTime($type, $time)
                    ->select();
                $result[$key]['number'] = count($dataRes);
                $result[$key]['row'] = 0;
                $result[$key]['clock'] = 0;
                $result[$key]['call'] = 0;
                $result[$key]['add'] = 0;
                $result[$key]['money'] = 0;
                foreach ($dataRes as $k => $v) {
                    $result[$key]['money'] += $v['money'];
                    if ($v['status'] == '1') {
                        $result[$key]['row'] += 1;
                    }
                    if ($v['status'] == '2') {
                        $result[$key]['clock'] += 1;
                    }
                    if ($v['status'] == '3') {
                        $result[$key]['call'] += 1;
                    }
                    if ($v['status'] == '4') {
                        $result[$key]['add'] += 1;
                    }
                }
                $this->result('查询成功', '', 200);

            }
        }
    }


    /**
     * 店铺商品统计
     */
    public function storeProduct()
    {
        if ($this->request->isPost()) {
            $getData = $this->request->post();
            if (!isset($getData['time']) || empty($getData['time'])) {
                $this->result('缺少时间类型', '', 204);
            }
            $userInfo = $this->userInfo;
            //下单时间
            if ($getData['time'] == '3') {
                //本月
                $time = 'month';
            }
            if ($getData['time'] == '4') {
                //上月
                $time = 'last month';
            }
            if ($getData['type'] == '1') {
                $type = 'cl.created_time';
            }
            if ($getData['type'] == '2') {
                $type = 'cl.updated_time';
            }
            $groupRes = Db::name('qt_commission_log')
                ->field('cl.*，p.name')
                ->alias('cl')
                ->leftJoin('qt_project p', 'cl.product_id = p.id')
                ->where(['cl.store_id' => $userInfo['store_id'], 'cl.type' => '2'])
                ->whereTime($type, $time)
                ->group('cl.product_id')
                ->select();
            $result = [];
            foreach ($groupRes as $key => $value) {
                $result[$key]['name'] = $value['name'];
                $dataRes = Db::name('qt_commission_log')
                    ->alias('cl')
                    ->leftJoin('qt_product p', 'cl.product_id = p.id')
                    ->where(['cl.store_id' => $userInfo['store_id'], 'cl.type' => '1', 'cl.product_id' => $value['product_id']])
                    ->whereTime($type, $time)
                    ->select();
                $result[$key]['number'] = count($dataRes);
                $result[$key]['row'] = 0;
                $result[$key]['clock'] = 0;
                $result[$key]['call'] = 0;
                $result[$key]['add'] = 0;
                $result[$key]['money'] = 0;
                foreach ($dataRes as $k => $v) {
                    $result[$key]['money'] += $v['money'];
                    if ($v['status'] == '1') {
                        $result[$key]['row'] += 1;
                    }
                    if ($v['status'] == '2') {
                        $result[$key]['clock'] += 1;
                    }
                    if ($v['status'] == '3') {
                        $result[$key]['call'] += 1;
                    }
                    if ($v['status'] == '4') {
                        $result[$key]['add'] += 1;
                    }
                }
                $this->result('查询成功', '', 200);
            }
        }
    }

    /**
     * 获取售卡统计
     */
    public function storeCard()
    {
        if ($this->request->isPost()) {
            $getData = $this->request->post();
            if (!isset($getData['time']) || empty($getData['time'])) {
                $this->result('缺少时间类型', '', 204);
            }
            $userInfo = $this->userInfo;
            //下单时间
            if ($getData['time'] == '3') {
                //本月
                $time = 'month';
            }
            if ($getData['time'] == '4') {
                //上月
                $time = 'last month';
            }
            if ($getData['type'] == '1') {
                $type = 'cl.created_time';
            }
            if ($getData['type'] == '2') {
                $type = 'cl.updated_time';
            }
            $groupRes = Db::name('qt_commission_log')
                ->field('cl.*，p.name')
                ->alias('cl')
                ->leftJoin('qt_viper_card c', 'cl.product_id = c.id')
                ->where(['cl.store_id' => $userInfo['store_id'], 'cl.type' => '3'])
                ->whereTime($type, $time)
                ->group('cl.product_id')
                ->select();
            $result = [];
            foreach ($groupRes as $key => $value) {
                $result[$key]['name'] = $value['name'];
                $dataRes = Db::name('qt_commission_log')
                    ->alias('cl')
                    ->leftJoin('qt_product p', 'cl.product_id = p.id')
                    ->where(['cl.store_id' => $userInfo['store_id'], 'cl.type' => '1', 'cl.product_id' => $value['product_id']])
                    ->whereTime($type, $time)
                    ->select();
                $result[$key]['number'] = count($dataRes);
                $result[$key]['row'] = 0;
                $result[$key]['clock'] = 0;
                $result[$key]['call'] = 0;
                $result[$key]['add'] = 0;
                $result[$key]['money'] = 0;
                foreach ($dataRes as $k => $v) {
                    $result[$key]['money'] += $v['money'];
                    if ($v['status'] == '1') {
                        $result[$key]['row'] += 1;
                    }
                    if ($v['status'] == '2') {
                        $result[$key]['clock'] += 1;
                    }
                    if ($v['status'] == '3') {
                        $result[$key]['call'] += 1;
                    }
                    if ($v['status'] == '4') {
                        $result[$key]['add'] += 1;
                    }
                }
                $this->result('查询成功', $result, 200);
            }
        }
    }


    /**
     * 会员卡消费统计
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function storeUserConsumption()
    {
        if ($this->request->isPost()) {
            $getData = $this->request->post();
            if (!isset($getData['time']) || empty($getData['time'])) {
                $this->result('缺少时间类型', '', 204);
            }
            $userInfo = $this->userInfo;
            //下单时间
            if ($getData['time'] == '3') {
                //本月
                $time = 'month';
            }
            if ($getData['time'] == '4') {
                //上月
                $time = 'last month';
            }
            if ($getData['type'] == '1') {
                $type = 'cl.created_time';
            }
            if ($getData['type'] == '2') {
                $type = 'cl.updated_time';
            }
            $groupRes = Db::name('qt_commission_log')
                ->field('cl.*，p.name')
                ->alias('cl')
                ->leftJoin('qt_viper_card c', 'cl.product_id = c.id')
                ->where(['cl.store_id' => $userInfo['store_id'], 'cl.type' => '3'])
                ->whereTime($type, $time)
                ->group('cl.product_id')
                ->select();
            $result = [];
            foreach ($groupRes as $key => $value) {
                $result[$key]['name'] = $value['name'];
                $dataRes = Db::name('qt_commission_log')
                    ->alias('cl')
                    ->leftJoin('qt_product p', 'cl.product_id = p.id')
                    ->where(['cl.store_id' => $userInfo['store_id'], 'cl.type' => '1', 'cl.product_id' => $value['product_id']])
                    ->whereTime($type, $time)
                    ->select();
                $result[$key]['number'] = count($dataRes);
                $result[$key]['row'] = 0;
                $result[$key]['clock'] = 0;
                $result[$key]['call'] = 0;
                $result[$key]['add'] = 0;
                $result[$key]['money'] = 0;
                foreach ($dataRes as $k => $v) {
                    $result[$key]['money'] += $v['money'];
                    if ($v['status'] == '1') {
                        $result[$key]['row'] += 1;
                    }
                    if ($v['status'] == '2') {
                        $result[$key]['clock'] += 1;
                    }
                    if ($v['status'] == '3') {
                        $result[$key]['call'] += 1;
                    }
                    if ($v['status'] == '4') {
                        $result[$key]['add'] += 1;
                    }
                }
                $this->result('查询成功', $result, 200);
            }
        }
    }


    /**
     * 技师阶梯提成
     */
    public function calculateCommission()
    {
        if ($this->request->isPost()) {
            $getData = $this->request->post();
            $userInfo = $this->userInfo;
            if (!empty($getData['type'] == '1')) {
                $field = 'log.created_time';
                $sonField = 'created_time';
            } else {
                $field = 'log.updated_time';
                $sonField = 'updated_time';
            }
            if (empty($getData['start_time']) || empty($getData['end_time'])) {
                $this->result('缺少时间类型', '', 204);
            }
            $sql = Db::name('qt_commission_log')
                ->field('u.id,u.real_name,u.work_number')
                ->alias('log')
                ->leftJoin('qt_users u', 'u.id = log.user_id')
                ->where(['log.store_id' => $userInfo['store_id']])
                ->where('log.user_id','neq',0)
                ->whereBetweenTime($field, $getData['start_time'], $getData['end_time']);
            $result = $sql->group('log.user_id')
                ->where('log.user_id', 'neq', 0)
                ->paginate(10)->each(function ($item) use ($sonField, $userInfo, $getData) {
                     $item['row'] = 0;//排钟
                     $item['spot'] = 0;//点钟
                     $item['add'] = 0;//加钟
                     $item['call'] = 0;//call钟
                     $item['choose'] = 0;//选钟
                     $item['add_row'] = 0;//加班排钟
                     $item['add_spot'] = 0;//加班点钟
                     $item['add_add'] = 0;//加班加钟
                     $item['add_call'] = 0;//加班call钟
                     $item['add_choose'] = 0;//加班选钟
                     $item['commission'] = 0;//阶梯提成
                     $item['add_commission'] = 0;//额外提成
                     //正常钟合计
                     $order = Db::name('qt_commission_log')
                         ->field('count(product_id) as number,status,product_id as project_id')
                         ->where(['store_id' => $userInfo['store_id'], 'user_id' => $item['id'], 'type' => '1','is_add'=>'1'])
                         ->whereBetweenTime($sonField, $getData['start_time'], $getData['end_time'])
                         ->where('user_id','neq',0)
                         ->group('status')
                         ->select();
                     foreach ($order as $k=>$v){
                         if($v['status'] == '1'){
                             $item['row'] = intval($v['number']);
                         }
                         if($v['status'] == '2'){
                             $item['spot'] = intval($v['number']);
                         }
                         if($v['status'] == '5'){
                             $item['add'] = intval($v['number']);
                         }
                         if($v['status'] == '3'){
                             $item['call'] = intval($v['number']);
                         }
                         if($v['status'] == '4'){
                             $item['choose'] = intval($v['number']);
                         }
                     }
                    $comData = Db::name('qt_commission_log')
                        ->alias('log')
                        ->leftJoin('qt_project p','p.id = log.product_id')
                        ->field('count(log.product_id) as number,log.status,log.product_id as project_id,p.name as project_name,log.is_add')
                        ->where(['log.store_id' => $userInfo['store_id'], 'log.user_id' => $item['id'], 'log.type' => '1','log.is_add'=>'1'])
                        ->whereBetweenTime($sonField, $getData['start_time'], $getData['end_time'])
                        ->where('log.user_id','neq',0)
                        ->group('log.product_id')
                        ->select();
                    $item['project_data'] = $comData;
                    $comData = unique_2d_array_by_key($comData,'project_id');
                    $commission = \app\common\model\EngineerConfig::calculateCommission($comData);
                    $item['commission'] += $commission['commission'];
                    $item['add_commission']  += $commission['add_commission'];
                    //加班钟合计
                    $addOrder = Db::name('qt_commission_log')
                        ->field('sum(product_id) as number,status, product_id as project_id')
                        ->where(['store_id' => $userInfo['store_id'], 'user_id' => $item['id'], 'type' => '1','is_add'=>'2'])
                        ->whereBetweenTime($sonField, $getData['start_time'], $getData['end_time'])
                        ->group('status')
                        ->select();
                    foreach ($addOrder as $k=>$v){
                        if($v['status'] == '1'){
                            $item['add_row'] = intval($v['number']);

                        }
                        if($v['status'] == '2'){
                            $item['add_spot'] = $v['number'];
                        }
                        if($v['status'] == '5'){
                            $item['add_add'] = intval($v['number']);
                        }
                        if($v['status'] == '3'){
                            $item['add_call'] = intval($v['number']);
                        }
                        if($v['status'] == '4'){
                            $item['add_choose'] = intval($v['number']);
                        }
                        $comData = Db::name('qt_commission_log')
                            ->alias('log')
                            ->leftJoin('qt_project p','p.id = log.product_id')
                            ->field('sum(log.product_id) as number,log.status,log.product_id as project_id,p.name as project_name,log.is_add')
                            ->where(['log.store_id' => $userInfo['store_id'], 'log.user_id' => $item['id'], 'log.type' => '1','log.is_add'=>'2'])
                            ->whereBetweenTime($sonField, $getData['start_time'], $getData['end_time'])
                            ->group('log.product_id')
                            ->select();
                        $item['project_data'] = $comData;
                        foreach ($comData as $kk=>$vv){
                            if($vv['status']='1'){
                                $commission = \app\common\model\EngineerConfig::calculateCommission($vv['project_id'],'6',$vv['number']);
                                $item['commission'] += $commission['commission'];
                                $item['add_commission']  += $commission['add_commission'];
                            }
                            if($vv['status']='2'){
                                $commission = \app\common\model\EngineerConfig::calculateCommission($vv['project_id'],'7',$vv['number']);
                                $item['commission'] += $commission['commission'];
                                $item['add_commission']  += $commission['add_commission'];
                            }

                        }
                    }
                    //商品详情
                    $goodsData = Db::name('qt_commission_log')
                        ->alias('log')
                        ->leftJoin('qt_product_order_item item','item.id = log.order_project_id')
                        ->field('sum(log.product_id) as number,log.product_id as goods_id,item.goods_name')
                        ->where(['log.store_id' => $userInfo['store_id'], 'log.user_id' => $item['id'], 'log.type' => '2'])
                        ->whereBetweenTime($sonField, $getData['start_time'], $getData['end_time'])
                        ->group('log.product_id')
                        ->select();
                    $item['goods_data'] = $goodsData;
                    $item['total_row'] = $item['row'] + $item['add_row'] + $item['add_choose'] + $item['choose'];
                    $item['total_spot'] = $item['add_spot'] + $item['spot'];
                    $item['total_add'] = $item['add_spot'] + $item['add_row'] + $item['add_choose'];
                    $item['total_choose'] = $item['choose'] + $item['add_choose'];
                    $item['total_man'] = Db::name('qt_commission_log')
                        ->alias('log')
                        ->leftJoin('qt_project p','p.id = log.product_id')
                        ->where(['log.store_id' => $userInfo['store_id'], 'log.user_id' => $item['id'], 'log.type' => '1','p.type'=>'1'])
                        ->whereBetweenTime($sonField, $getData['start_time'], $getData['end_time'])
                        ->count();
                    $item['total_other'] = Db::name('qt_commission_log')
                        ->alias('log')
                        ->leftJoin('qt_project p','p.id = log.product_id')
                        ->where(['log.store_id' => $userInfo['store_id'], 'log.user_id' => $item['id'], 'log.type' => '1','p.type'=>'2'])
                        ->whereBetweenTime($sonField, $getData['start_time'], $getData['end_time'])
                        ->count();

                    $item['total_man_commission'] = Db::name('qt_commission_log')
                        ->alias('log')
                        ->leftJoin('qt_project p','p.id = log.product_id')
                        ->where(['log.store_id' => $userInfo['store_id'], 'log.user_id' => $item['id'], 'log.type' => '1','p.type'=>'1'])
                        ->whereBetweenTime($sonField, $getData['start_time'], $getData['end_time'])
                        ->sum('commission');
                    $item['total_other_commission'] = Db::name('qt_commission_log')
                        ->alias('log')
                        ->leftJoin('qt_project p','p.id = log.product_id')
                        ->where(['log.store_id' => $userInfo['store_id'], 'log.user_id' => $item['id'], 'log.type' => '1','p.type'=>'2'])
                        ->whereBetweenTime($sonField, $getData['start_time'], $getData['end_time'])
                        ->sum('commission');
                    //售卡金额
                    $item['total_card_money'] = Db::name('qt_commission_log')
                        ->where(['store_id' => $userInfo['store_id']])
                        ->whereBetweenTime($sonField, $getData['start_time'], $getData['end_time'])
                        ->where(['user_id' => $item['id']])
                        ->whereIn('type',['3','4','5','7'])
                        ->sum('money');
                    //售卡提成
                    $item['total_card_commission'] = Db::name('qt_commission_log')
                        ->where(['store_id' => $userInfo['store_id']])
                        ->whereBetweenTime($sonField, $getData['start_time'], $getData['end_time'])
                        ->where(['user_id' => $item['id']])
                        ->whereIn('type',['3','4','5','7'])
                        ->sum('commission');
                    //商品提成
                    $item['total_goods_commission'] = Db::name('qt_commission_log')
                        ->where(['store_id' => $userInfo['store_id']])
                        ->whereBetweenTime($sonField, $getData['start_time'], $getData['end_time'])
                        ->where(['user_id' => $item['id'], 'type' => '2'])
                        ->sum('commission');
                    //推荐提成
                    $item['total_tuijian'] = Db::name('qt_commission_log')
                        ->where(['store_id' => $userInfo['store_id']])
                        ->whereBetweenTime($sonField, $getData['start_time'], $getData['end_time'])
                        ->where(['user_id' => $item['id'], 'type' => '8'])
                        ->sum('commission');
                    $totalCommission = Db::name('qt_commission_log')
                        ->where(['store_id' => $userInfo['store_id']])
                        ->whereBetweenTime($sonField, $getData['start_time'], $getData['end_time'])
                        ->where(['user_id' => $item['id']])
                        ->sum('commission');
                    $item['total_commission'] = $totalCommission + $item['commission'] + $item['add_commission'];

                    return $item;
                });
                $this->result('查询成功', $result, 200);
            }
            $this->result('无效的请求', '', 204);
    }


    /**
     * 获取柱状图
     * @param $data
     */
    public function histogram($data)
    {
        foreach ($data as $key => $value) {
            $categories[$key] = $value['name'];
            $money[$key] = $value['value'];
        }
        $result['histogram']['categories'] = $categories;
        $result['histogram']['name'] = '金额';
        $result['histogram']['data'] = $money;
        return $result;
    }


    /**
     * 获取会员卡数据统计
     * @param $storeId [门店ID]
     * @param $time [时间类型] [1 今天 2 昨天 3 本月 4 上月]
     * @param $type [类型] [1会员充值 2 会员消费]
     */
    public function getCardTotal($storeId, $time, $type)
    {
        //会员充值
        if ($type == '1') {
            $in = ['3', '4'];
        }
        //会员消费
        if ($type == '2') {
            $in = ['1', '2', '5'];
        }
        //会员充值
        $result['totalCardPrice'] = Db::name('qt_commission_log')
            ->where('store_id', $storeId)
            ->where('type', 'in', ['3', '4'])
            ->whereTime('updated_time', $time)
            ->sum('money');
        $careType = Db::name('qt_viper_type')->where(['store_id' => $storeId])->select();
        foreach ($careType as $key => $value) {
            $result['series'][$key]['name'] = $value['name'];
            $result['series'][$key]['value'] = Db::name('qt_commission_log')
                ->where('store_id', $storeId)
                ->where('type', 'in', $in)
                ->where('card_type_id', $value['id'])
                ->whereTime('updated_time', $time)
                ->sum('money');
        }
        return $result;
    }



    /**
     * 技师实收业绩报表单个详情
     */
    public function netReceiptsSingleDetail()
    {
        if ($this->request->isPost()) {
            $getData = $this->request->post();
            $userInfo = $this->userInfo;
            $leftField = 'log.created_time';
            $startTime = strtotime($getData['start_time']);
            $endTime = strtotime($getData['end_time']);
            $projectDetailData = Db::name('qt_commission_log')
                ->field('log.product_id as project_id,p.price,p.name as project_name,op.order_type,r.name as room_name,u.work_number,log.commission,log.payment_price,log.created_time,log.updated_time')
                ->alias('log')
                ->leftJoin('qt_project p', 'p.id = log.product_id')
                ->leftJoin('qt_order_project op', 'op.id = log.order_project_id')
                ->leftJoin('qt_room r', 'r.id = op.room_id')
                ->leftJoin('qt_users u', 'u.id = op.engineer_id')
                ->where(['log.store_id' => $userInfo['store_id'], 'log.user_id' => $getData['user_id']])
                ->whereBetweenTime($leftField, $startTime, $endTime)
                ->where('log.type', '1')
                ->select();
            $cardData = Db::name('qt_viper_card_recharge_log')
                ->field('type.name as viper_type_name,card.name,card.card_no,card.mobile,log.recharge_temp_name,log.pay_content,log.commission,log.create_time ')
                ->alias('log')
                ->leftJoin('qt_viper_card card', 'card.id = log.viper_card_id')
                ->leftJoin('qt_viper_type type', 'type.id = card.viper_type_id')
                ->where(['log.store_id' => $userInfo['store_id'], 'log.sale_id' => $getData['user_id']])
                ->whereBetweenTime('log.create_time', $startTime, $endTime)
                ->select();

            $goodsData = Db::name('qt_commission_log')
                ->field('g.name as goods_name,r.name as room_name,o.goods_price,o.goods_num,o.payment_price,log.commission,log.created_time,log.updated_time')
                ->alias('log')
                ->leftJoin('qt_goods g', 'g.id = log.product_id')
                ->leftJoin('qt_product_order_item o', 'o.id = log.order_project_id')
                ->leftJoin('qt_room r', 'r.id = o.room_id')
                ->where(['log.store_id' => $userInfo['store_id'], 'log.user_id' => $getData['user_id']])
                ->whereBetweenTime('log.created_time', $startTime, $endTime)
                ->where('log.type', '2')
                ->select();
            $result['project_data'] = $projectDetailData;
            $result['card_data'] = $cardData;
            $result['goods_data'] = $goodsData;
            $this->result('查询成功', $result, 200);
        }
        $this->result('无效的请求', '', 204);
    }

}
